home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / frasrc19.zip / CALCFRAC.C < prev    next >
C/C++ Source or Header  |  1995-03-08  |  99KB  |  3,305 lines

  1. /*
  2. CALCFRAC.C contains the high level ("engine") code for calculating the
  3. fractal images (well, SOMEBODY had to do it!).
  4. Original author Tim Wegner, but just about ALL the authors have contributed
  5. SOME code to this routine at one time or another, or contributed to one of
  6. the many massive restructurings.
  7. The following modules work very closely with CALCFRAC.C:
  8.   FRACTALS.C    the fractal-specific code for escape-time fractals.
  9.   FRACSUBR.C    assorted subroutines belonging mainly to calcfrac.
  10.   CALCMAND.ASM    fast Mandelbrot/Julia integer implementation
  11. Additional fractal-specific modules are also invoked from CALCFRAC:
  12.   LORENZ.C    engine level and fractal specific code for attractors.
  13.   JB.C        julibrot logic
  14.   PARSER.C    formula fractals
  15.   and more
  16.  -------------------------------------------------------------------- */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <float.h>
  22. #include <limits.h>
  23. #ifndef XFRACT
  24. #include <dos.h>
  25. #endif
  26. #include "fractint.h"
  27. #include "fractype.h"
  28. #include "mpmath.h"
  29. #include "targa_lc.h"
  30. #include "prototyp.h"
  31. #include "biginit.h"
  32.  
  33. /* routines in this module    */
  34.  
  35. static void perform_worklist(void);
  36. static int  OneOrTwoPass(void);
  37. static int  _fastcall StandardCalc(int);
  38. static int  _fastcall potential(double,long);
  39. static void decomposition(void);
  40. static int  bound_trace_main(void);
  41. static void step_col_row(void);
  42. static int  solidguess(void);
  43. static int  _fastcall guessrow(int,int,int);
  44. static void _fastcall plotblock(int,int,int,int);
  45. static void _fastcall setsymmetry(int,int);
  46. static int  _fastcall xsym_split(int,int);
  47. static int  _fastcall ysym_split(int,int);
  48.  
  49. /**CJLT new function prototypes: */
  50. static int tesseral(void);
  51. static int _fastcall tesschkcol(int,int,int);
  52. static int _fastcall tesschkrow(int,int,int);
  53. static int _fastcall tesscol(int,int,int);
  54. static int _fastcall tessrow(int,int,int);
  55. /* added for testing autologmap() */
  56. static int autologmap(void);
  57.  
  58. _LCMPLX linitorbit;
  59. long lmagnitud, llimit, llimit2, lclosenuff, l16triglim;
  60. _CMPLX init,tmp,old,new,saved;
  61. int color;
  62. long coloriter, oldcoloriter, realcoloriter;
  63. int row, col, passes;
  64. int iterations, invert;
  65. double f_radius,f_xcenter, f_ycenter; /* for inversion */
  66.  
  67. void (_fastcall *plot)(int,int,int) = putcolor;
  68. typedef void (_fastcall *PLOT)(int,int,int);
  69.  
  70. double        min_orbit;        /* orbit value closest to origin */
  71. long        min_index;        /* iteration of min_orbit */
  72. double /*deltaX,*/ deltaY;
  73. double magnitude, rqlim, rqlim2, rqlim_save;
  74. int no_mag_calc;
  75. int use_old_period;
  76. int use_old_distest;
  77. int old_demm_colors;
  78. int (*calctype)(void);
  79. int (*calctypetmp)(void);
  80.  
  81. double closenuff;
  82. int pixelpi; /* value of pi in pixels */
  83. unsigned long lm;        /* magnitude limit (CALCMAND) */
  84.  
  85. /* ORBIT variables */
  86. int    show_orbit;            /* flag to turn on and off */
  87. int    orbit_ptr;            /* pointer into save_orbit array */
  88. int far *save_orbit;            /* array to save orbit values */
  89. int    orbit_color=15;         /* XOR color */
  90.  
  91. int    ixstart, ixstop, iystart, iystop;    /* start, stop here */
  92. int    symmetry;       /* symmetry flag */
  93. int    reset_periodicity; /* nonzero if escape time pixel rtn to reset */
  94. int    kbdcount, max_kbdcount;    /* avoids checking keyboard too often */
  95.  
  96. char far *resume_info = NULL;        /* pointer to resume info if allocated */
  97. int resuming;                /* nonzero if resuming after interrupt */
  98. int num_worklist;            /* resume worklist for standard engine */
  99. WORKLIST worklist[MAXCALCWORK];
  100. int xxstart,xxstop;            /* these are same as worklist, */
  101. int yystart,yystop,yybegin;        /* declared as separate items  */
  102. int workpass,worksym;            /* for the sake of calcmand    */
  103.  
  104. VOIDFARPTR typespecific_workarea = NULL;
  105.  
  106. static double dem_delta, dem_width;    /* distance estimator variables */
  107. static double dem_toobig;
  108. static int dem_mandel;
  109. #define DEM_BAILOUT 535.5  /* (pb: not sure if this is special or arbitrary) */
  110.  
  111. /* variables which must be visible for tab_display */
  112. int got_status; /* -1 if not, 0 for 1or2pass, 1 for ssg, 2 for btm, 3 for 3d */
  113. int curpass,totpasses;
  114. int currow,curcol;
  115.  
  116. /* static vars for solidguess & its subroutines */
  117. char three_pass;
  118. static int maxblock,halfblock;
  119. static int guessplot;            /* paint 1st pass row at a time?   */
  120. static int right_guess,bottom_guess;
  121. #define maxyblk 7    /* maxxblk*maxyblk*2 <= 4096, the size of "prefix" */
  122. #define maxxblk 202  /* each maxnblk is oversize by 2 for a "border" */
  123.              /* maxxblk defn must match fracsubr.c */
  124. /* next has a skip bit for each maxblock unit;
  125.    1st pass sets bit  [1]... off only if block's contents guessed;
  126.    at end of 1st pass [0]... bits are set if any surrounding block not guessed;
  127.    bits are numbered [..][y/16+1][x+1]&(1<<(y&15)) */
  128.  
  129. /* Original array */
  130. /* extern unsigned int prefix[2][maxyblk][maxxblk]; */
  131.  
  132. typedef int (*TPREFIX)[2][maxyblk][maxxblk];
  133. #define tprefix   (*((TPREFIX)prefix))
  134.  
  135. /* size of next puts a limit of MAXPIXELS pixels across on solid guessing logic */
  136. #ifdef XFRACT
  137. BYTE dstack[4096];              /* common temp, two put_line calls */
  138. unsigned int prefix[2][maxyblk][maxxblk]; /* common temp */
  139. #endif
  140.  
  141. int nxtscreenflag; /* for cellular next screen generation */
  142. int    attractors;            /* number of finite attractors  */
  143. _CMPLX    attr[N_ATTR];        /* finite attractor vals (f.p)  */
  144. _LCMPLX lattr[N_ATTR];        /* finite attractor vals (int)  */
  145. int    attrperiod[N_ATTR];        /* period of the finite attractor */
  146.  
  147. /***** vars for new btm *****/
  148. enum direction {North,East,South,West};
  149. enum direction going_to;
  150. int trail_row, trail_col;
  151.  
  152. #ifndef sqr
  153. #define sqr(x) ((x)*(x))
  154. #endif
  155.  
  156. #ifndef lsqr
  157. #define lsqr(x) (multiply((x),(x),bitshift))
  158. #endif
  159.  
  160. /* -------------------------------------------------------------------- */
  161. /*        These variables are external for speed's sake only      */
  162. /* -------------------------------------------------------------------- */
  163.  
  164. int periodicitycheck;
  165.  
  166. /* For periodicity testing, only in StandardFractal() */
  167. int nextsavedincr;
  168. long firstsavedand;
  169.  
  170. int calctypeshowdot(void)
  171. {
  172.    (*plot) (col, row, showdot%colors);
  173.    return((*calctypetmp)());
  174. }
  175.  
  176. /* use top of extraseg for LogTable if room */
  177. int logtable_in_extra_ok(void)
  178. {
  179.    if(((2L*(long)(xdots+ydots)*sizeof(double)+MaxLTSize+1) < (1L<<16)) && (bf_math==0))
  180.       return(1);
  181.    else
  182.       return(0);
  183. }
  184.  
  185. /******* calcfract - the top level routine for generating an image *******/
  186.  
  187. #if (_MSC_VER >= 700)
  188. #pragma code_seg ("calcfra1_text")     /* place following in an overlay */
  189. #endif
  190.  
  191. int calcfract(void)
  192. {
  193.    first_err = 1;
  194.    attractors = 0;        /* default to no known finite attractors  */
  195.    display3d = 0;
  196.    basin = 0;
  197.  
  198.    init_misc();  /* set up some variables in parser.c */
  199.  
  200.    /* following delta values useful only for types with rotation disabled */
  201.    /* currently used only by bifurcation */
  202.    if (integerfractal)
  203.    {
  204.       distest = 0;
  205.       /* deltaX = (double)lx0[     1] / fudge - xxmin;*/
  206.       deltaY = yymax - (double)ly0[     1] / fudge;
  207.    }
  208.    else
  209.    {
  210.       /* deltaX = dx0[     1] - xxmin;*/
  211.       deltaY = yymax - dy0[     1];
  212.    }
  213.  
  214.    parm.x   = param[0];
  215.    parm.y   = param[1];
  216.    parm2.x  = param[2];
  217.    parm2.y  = param[3];
  218.  
  219.    if (LogFlag && colors < 16) {
  220.       static FCODE msg[]={"Need at least 16 colors to use logmap"};
  221.       stopmsg(0,msg);
  222.       LogFlag = 0;
  223.       }
  224.  
  225.    if (use_old_period == 1) {
  226.       nextsavedincr = 1;
  227.       firstsavedand = 1;
  228.    }
  229.    else {
  230.       nextsavedincr = (int)log10(maxit); /* works better than log() */
  231.       if(nextsavedincr < 4) nextsavedincr = 4; /* maintains image with low iterations */
  232.       firstsavedand = (long)((nextsavedincr*2) + 1);
  233.    }
  234.  
  235.    if(maxit > 32767)
  236.       MaxLTSize = 32767;
  237.    else
  238.       MaxLTSize = (int)maxit;
  239.  
  240.    if (LogFlag || rangeslen)
  241.    {
  242.       LogTable = NULL;
  243.       if(logtable_in_extra_ok())
  244.          LogTable = (BYTE far *)(dx0 + 2*(xdots+ydots));
  245.       else 
  246.          LogTable = farmemalloc((long)MaxLTSize + 1); 
  247.  
  248.       if(LogTable == NULL)
  249.       {
  250.      static FCODE msg[]={"Insufficient memory for logmap/ranges with this maxiter"};
  251.          stopmsg(0,msg);
  252.          LogFlag = 0;
  253.       }
  254.       else if (rangeslen) {
  255.      int i,k,l,m,numval,flip,altern;
  256.      i = k = l = 0;
  257.      while (i < rangeslen) {
  258.         m = flip = 0;
  259.         altern = 32767;
  260.         if ((numval = ranges[i++]) < 0) {
  261.            altern = ranges[i++];    /* sub-range iterations */
  262.            numval = ranges[i++];
  263.            }
  264.         if (numval > MaxLTSize || i >= rangeslen)
  265.            numval = MaxLTSize;
  266.         while (l <= numval)  {
  267.            LogTable[l++] = (BYTE)(k + flip);
  268.            if (++m >= altern) {
  269.           flip ^= 1;        /* Alternate colors */
  270.           m = 0;
  271.           }
  272.            }
  273.         ++k;
  274.         if (altern != 32767) ++k;
  275.         }
  276.      }
  277.       else
  278.      SetupLogTable();
  279.    }
  280.    lm = 4L << bitshift;         /* CALCMAND magnitude limit */
  281.  
  282.    /* ORBIT stuff */
  283.    save_orbit = (int far *)((double huge *)dx0 + 4*MAXPIXELS);
  284.    show_orbit = 0;
  285.    orbit_ptr = 0;
  286.    orbit_color = 15;
  287.    if(colors < 16)
  288.       orbit_color = 1;
  289.  
  290.    if(inversion[0] != 0.0)
  291.    {
  292.       f_radius      = inversion[0];
  293.       f_xcenter   = inversion[1];
  294.       f_ycenter   = inversion[2];
  295.  
  296.       if (inversion[0] == AUTOINVERT)  /*  auto calc radius 1/6 screen */
  297.      inversion[0] = f_radius = min(fabs(xxmax - xxmin),
  298.          fabs(yymax - yymin)) / 6.0;
  299.  
  300.       if (invert < 2 || inversion[1] == AUTOINVERT)  /* xcenter not already set */
  301.       {
  302.      inversion[1] = f_xcenter = (xxmin + xxmax) / 2.0;
  303.      if (fabs(f_xcenter) < fabs(xxmax-xxmin) / 100)
  304.         inversion[1] = f_xcenter = 0.0;
  305.       }
  306.  
  307.       if (invert < 3 || inversion[2] == AUTOINVERT)  /* ycenter not already set */
  308.       {
  309.      inversion[2] = f_ycenter = (yymin + yymax) / 2.0;
  310.      if (fabs(f_ycenter) < fabs(yymax-yymin) / 100)
  311.         inversion[2] = f_ycenter = 0.0;
  312.       }
  313.  
  314.       invert = 3; /* so values will not be changed if we come back */
  315.    }
  316.  
  317.    closenuff = ddelmin*pow(2.0,-(double)(abs(periodicitycheck)));
  318.    rqlim_save = rqlim;
  319.    rqlim2 = sqrt(rqlim);
  320.    if (integerfractal)        /* for integer routines (lambda) */
  321.    {
  322.       lparm.x = (long)(parm.x * fudge);    /* real portion of Lambda */
  323.       lparm.y = (long)(parm.y * fudge);    /* imaginary portion of Lambda */
  324.       lparm2.x = (long)(parm2.x * fudge);  /* real portion of Lambda2 */
  325.       lparm2.y = (long)(parm2.y * fudge);  /* imaginary portion of Lambda2 */
  326.       llimit = (long)(rqlim * fudge);      /* stop if magnitude exceeds this */
  327.       if (llimit <= 0) llimit = 0x7fffffffL; /* klooge for integer math */
  328.       llimit2 = (long)(rqlim2 * fudge);    /* stop if magnitude exceeds this */
  329.       lclosenuff = (long)(closenuff * fudge); /* "close enough" value */
  330.       l16triglim = 8L<<16;       /* domain limit of fast trig functions */
  331.       linitorbit.x = (long)(initorbit.x * fudge);
  332.       linitorbit.y = (long)(initorbit.y * fudge);
  333.    }
  334.    resuming = (calc_status == 2);
  335.    if (!resuming) /* free resume_info memory if any is hanging around */
  336.    {
  337.       end_resume();
  338.       if (resave_flag) {
  339.      updatesavename(savename); /* do the pending increment */
  340.      resave_flag = started_resaves = 0;
  341.      }
  342.       calctime = 0;
  343.    }
  344.  
  345.    if (curfractalspecific->calctype != StandardFractal
  346.        && curfractalspecific->calctype != calcmand
  347.        && curfractalspecific->calctype != calcmandfp
  348.        && curfractalspecific->calctype != lyapunov
  349.        && curfractalspecific->calctype != calcfroth)
  350.    {
  351.       calctype = curfractalspecific->calctype; /* per_image can override */
  352.       symmetry = curfractalspecific->symmetry; /*   calctype & symmetry  */
  353.       plot = putcolor; /* defaults when setsymmetry not called or does nothing */
  354.       iystart = ixstart = yystart = xxstart = yybegin = 0;
  355.       iystop = yystop = ydots -1;
  356.       ixstop = xxstop = xdots -1;
  357.       calc_status = 1; /* mark as in-progress */
  358.       distest = 0; /* only standard escape time engine supports distest */
  359.       /* per_image routine is run here */
  360.       if (curfractalspecific->per_image())
  361.       { /* not a stand-alone */
  362.      /* next two lines in case periodicity changed */
  363.      closenuff = ddelmin*pow(2.0,-(double)(abs(periodicitycheck)));
  364.      lclosenuff = (long)(closenuff * fudge); /* "close enough" value */
  365.      setsymmetry(symmetry,0);
  366.      timer(0,calctype); /* non-standard fractal engine */
  367.       }
  368.       if (check_key())
  369.       {
  370.      if (calc_status == 1) /* calctype didn't set this itself, */
  371.         calc_status = 3;   /* so mark it interrupted, non-resumable */
  372.       }
  373.       else
  374.      calc_status = 4; /* no key, so assume it completed */
  375.    }
  376.    else /* standard escape-time engine */
  377.    {
  378.       if(stdcalcmode == '3')  /* convoluted 'g' + '2' hybrid */
  379.       {
  380.          int oldcalcmode;
  381.          oldcalcmode = stdcalcmode;
  382.          if(!resuming || three_pass)
  383.          {
  384.             stdcalcmode = 'g';
  385.             three_pass = 1;
  386.             timer(0,(int(*)())perform_worklist);
  387.             if(calc_status == 4)
  388.             {
  389.                if(xdots >= 640)  /* '2' is silly after 'g' for low rez */
  390.                   stdcalcmode = '2';
  391.                else
  392.                   stdcalcmode = '1';
  393.                   
  394.                timer(0,(int(*)())perform_worklist);
  395.                three_pass = 0;
  396.             }   
  397.          }
  398.          else /* resuming '2' pass */
  399.          { 
  400.             if(xdots >= 640)
  401.                stdcalcmode = '2';
  402.             else
  403.                stdcalcmode = '1';
  404.             timer(0,(int(*)())perform_worklist);
  405.          }
  406.          stdcalcmode = (char)oldcalcmode;
  407.       }
  408.       else /* main case, much nicer! */
  409.       {
  410.          three_pass = 0;
  411.          timer(0,(int(*)())perform_worklist);
  412.       }
  413.    }
  414.    calctime += timer_interval;
  415.    
  416.    if(LogTable)
  417.    {
  418.       if(!logtable_in_extra_ok())
  419.          farmemfree(LogTable);   /* free if not using extraseg */
  420.       LogTable = NULL;
  421.    }
  422.    if(typespecific_workarea)
  423.    {
  424.       free_workarea();
  425.    }
  426.  
  427.    if (curfractalspecific->calctype == calcfroth)
  428.       froth_cleanup();
  429.  
  430.    return((calc_status == 4) ? 0 : -1);
  431. }
  432.  
  433. /* locate alternate math record */
  434. int find_alternate_math(int type, int math)
  435. {
  436.    int i,ret,curtype /* ,curmath=0 */;
  437.    /* unsigned umath; */
  438.    ret = -1;
  439.    if(math==0)
  440.       return(ret);
  441.    i= -1;
  442. #if 0  /* for now at least, the only alternatemath is bignum and bigflt math */
  443.    umath = math;
  444.    umath <<= 14;  /* BF_MATH or DL_MATH */
  445.    
  446.    /* this depends on last two bits of flags */
  447.    if(fractalspecific[type].flags & umath)
  448.    {
  449.       while(((curtype=alternatemath[++i].type ) != type  
  450.           || (curmath=alternatemath[i].math) != math) && curtype != -1);
  451.       if(curtype == type && curmath == math)
  452.         ret = i;
  453.    }
  454. #else
  455.    while ((curtype=alternatemath[++i].type) != type && curtype != -1)
  456.       ;
  457.    if(curtype == type && alternatemath[i].math)
  458.      ret = i;
  459. #endif
  460.    return(ret);
  461. }     
  462.  
  463.  
  464. /**************** general escape-time engine routines *********************/
  465.  
  466. static void perform_worklist()
  467. {
  468.    int (*sv_orbitcalc)(void) = NULL;  /* function that calculates one orbit */
  469.    int (*sv_per_pixel)(void) = NULL;  /* once-per-pixel init */
  470.    int (*sv_per_image)(void) = NULL;  /* once-per-image setup */
  471.    int i, alt;
  472.  
  473.    if((alt=find_alternate_math(fractype,bf_math)) > -1)
  474.    {
  475.       sv_orbitcalc = curfractalspecific->orbitcalc;
  476.       sv_per_pixel = curfractalspecific->per_pixel;
  477.       sv_per_image = curfractalspecific->per_image;
  478.       curfractalspecific->orbitcalc = alternatemath[alt].orbitcalc;
  479.       curfractalspecific->per_pixel = alternatemath[alt].per_pixel;
  480.       curfractalspecific->per_image = alternatemath[alt].per_image;
  481.    }
  482.    else
  483.       bf_math = 0;
  484.    
  485.    if (potflag && pot16bit)
  486.    {
  487.       int tmpcalcmode = stdcalcmode;
  488.  
  489.       stdcalcmode = '1'; /* force 1 pass */
  490.       if (resuming == 0)
  491.      if (pot_startdisk() < 0)
  492.      {
  493.         pot16bit = 0;    /* startdisk failed or cancelled */
  494.         stdcalcmode = (char)tmpcalcmode;    /* maybe we can carry on??? */
  495.      }
  496.    }
  497.    if (stdcalcmode == 'b' && (curfractalspecific->flags & NOTRACE))
  498.       stdcalcmode = '1';
  499.    if (stdcalcmode == 'g' && (curfractalspecific->flags & NOGUESS))
  500.       stdcalcmode = '1';
  501.  
  502.    /* default setup a new worklist */
  503.    num_worklist = 1;
  504.    worklist[0].xxstart = 0;
  505.    worklist[0].yystart = worklist[0].yybegin = 0;
  506.    worklist[0].xxstop = xdots - 1;
  507.    worklist[0].yystop = ydots - 1;
  508.    worklist[0].pass = worklist[0].sym = 0;
  509.    if (resuming) /* restore worklist, if we can't the above will stay in place */
  510.    {
  511.       start_resume();
  512.       get_resume(sizeof(int),&num_worklist,sizeof(worklist),worklist,0);
  513.       end_resume();
  514.    }
  515.  
  516.    if (distest) /* setup stuff for distance estimator */
  517.    {
  518.       double ftemp,ftemp2,delxx,delyy2,delyy,delxx2,dxsize,dysize;
  519.       double aspect;
  520.       if(pseudox && pseudoy)
  521.       {
  522.          aspect = (double)pseudoy/(double)pseudox;
  523.          dxsize = pseudox-1;
  524.          dysize = pseudoy-1;
  525.       }   
  526.       else
  527.       {
  528.          aspect = (double)ydots/(double)xdots;
  529.          dxsize = xdots-1;
  530.          dysize = ydots-1;
  531.       }   
  532.       
  533.       delxx  = (xxmax - xx3rd) / dxsize; /* calculate stepsizes */
  534.       delyy  = (yymax - yy3rd) / dysize;
  535.       delxx2 = (xx3rd - xxmin) / dysize;
  536.       delyy2 = (yy3rd - yymin) / dxsize;
  537.  
  538.       if (save_release < 1827) /* in case it's changed with <G> */
  539.          use_old_distest = 1;
  540.       else
  541.          use_old_distest = 0;
  542.       rqlim = rqlim_save; /* just in case changed to DEM_BAILOUT earlier */
  543.       if (distest != 1 || colors == 2) /* not doing regular outside colors */
  544.          if (rqlim < DEM_BAILOUT)      /* so go straight for dem bailout */
  545.             rqlim = DEM_BAILOUT;
  546.       if (curfractalspecific->tojulia != NOFRACTAL || use_old_distest
  547.           || fractype == FORMULA || fractype == FFORMULA)
  548.          dem_mandel = 1; /* must be mandel type, formula, or old PAR/GIF */
  549.       else
  550.          dem_mandel = 0;
  551.       dem_delta = sqr(delxx) + sqr(delyy2);
  552.       if ((ftemp = sqr(delyy) + sqr(delxx2)) > dem_delta)
  553.      dem_delta = ftemp;
  554.       if (distestwidth == 0)
  555.      distestwidth = 1;
  556.       ftemp = distestwidth;
  557.       if (distestwidth > 0)
  558.          dem_delta *= sqr(ftemp)/10000; /* multiply by thickness desired */
  559.       else
  560.          dem_delta *= 1/(sqr(ftemp)*10000); /* multiply by thickness desired */
  561.       dem_width = ( sqrt( sqr(xxmax-xxmin) + sqr(xx3rd-xxmin) ) * aspect
  562.       + sqrt( sqr(yymax-yymin) + sqr(yy3rd-yymin) ) ) / distest;
  563.       ftemp = (rqlim < DEM_BAILOUT) ? DEM_BAILOUT : rqlim;
  564.       ftemp += 3; /* bailout plus just a bit */
  565.       ftemp2 = log(ftemp);
  566.       if(use_old_distest)
  567.          dem_toobig = sqr(ftemp) * sqr(ftemp2) * 4 / dem_delta;
  568.       else
  569.          dem_toobig = fabs(ftemp) * fabs(ftemp2) * 2 / sqrt(dem_delta);
  570.    }
  571.  
  572.    while (num_worklist > 0)
  573.    {
  574.       /* per_image can override */
  575.       calctype = curfractalspecific->calctype; 
  576.       symmetry = curfractalspecific->symmetry; /*   calctype & symmetry  */
  577.       plot = putcolor; /* defaults when setsymmetry not called or does nothing */
  578.  
  579.       /* pull top entry off worklist */
  580.       ixstart = xxstart = worklist[0].xxstart;
  581.       ixstop  = xxstop    = worklist[0].xxstop;
  582.       iystart = yystart = worklist[0].yystart;
  583.       iystop  = yystop    = worklist[0].yystop;
  584.       yybegin  = worklist[0].yybegin;
  585.       workpass = worklist[0].pass;
  586.       worksym  = worklist[0].sym;
  587.       --num_worklist;
  588.       for (i=0; i<num_worklist; ++i)
  589.      worklist[i] = worklist[i+1];
  590.  
  591.       calc_status = 1; /* mark as in-progress */
  592.  
  593.       curfractalspecific->per_image();
  594.       if(showdot > 0)
  595.       {
  596.          calctypetmp = calctype;
  597.          calctype = calctypeshowdot;
  598.       }
  599.  
  600.       /* some common initialization for escape-time pixel level routines */
  601.       closenuff = ddelmin*pow(2.0,-(double)(abs(periodicitycheck)));
  602.       lclosenuff = (long)(closenuff * fudge); /* "close enough" value */
  603.       kbdcount=max_kbdcount;
  604.  
  605.       setsymmetry(symmetry,1);
  606.  
  607. /* added for testing autologmap() */
  608.       if (!(resuming)&&(abs(LogFlag) ==2))
  609.        {  /* calculate round screen edges to work out best start for logmap */
  610.          LogFlag = ( autologmap() * (LogFlag / abs(LogFlag)));
  611.          SetupLogTable();
  612.        }
  613.  
  614.       /* call the appropriate escape-time engine */
  615.       switch (stdcalcmode)
  616.       {
  617.       case 't':
  618.      tesseral();
  619.      break;
  620.       case 'b':
  621.      bound_trace_main();
  622.      break;
  623.       case 'g':
  624.      solidguess();
  625.      break;
  626.       default:
  627.      OneOrTwoPass();
  628.       }
  629.  
  630.       if (check_key()) /* interrupted? */
  631.      break;
  632.    }
  633.  
  634.    if (num_worklist > 0)
  635.    {  /* interrupted, resumable */
  636.       alloc_resume(sizeof(worklist)+10,1);
  637.       put_resume(sizeof(int),&num_worklist,sizeof(worklist),worklist,0);
  638.    }
  639.    else
  640.       calc_status = 4; /* completed */
  641.    if(sv_orbitcalc != NULL)
  642.    {
  643.       curfractalspecific->orbitcalc = sv_orbitcalc;
  644.       curfractalspecific->per_pixel = sv_per_pixel;
  645.       curfractalspecific->per_image = sv_per_image;
  646.    }
  647. }
  648.  
  649. #if (_MSC_VER >= 700)
  650. #pragma code_seg ()     /* back to normal segment */
  651. #endif
  652.  
  653. static int OneOrTwoPass(void)
  654. {
  655.    int i;
  656.  
  657.    totpasses = 1;
  658.    if (stdcalcmode == '2') totpasses = 2;
  659.    if (stdcalcmode == '2' && workpass == 0) /* do 1st pass of two */
  660.    {
  661.       if (StandardCalc(1) == -1)
  662.       {
  663.      add_worklist(xxstart,xxstop,yystart,yystop,row,0,worksym);
  664.      return(-1);
  665.       }
  666.       if (num_worklist > 0) /* worklist not empty, defer 2nd pass */
  667.       {
  668.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,1,worksym);
  669.      return(0);
  670.       }
  671.       workpass = 1;
  672.       yybegin = yystart;
  673.    }
  674.    /* second or only pass */
  675.    if (StandardCalc(2) == -1)
  676.    {
  677.       i = yystop;
  678.       if (iystop != yystop) /* must be due to symmetry */
  679.      i -= row - iystart;
  680.       add_worklist(xxstart,xxstop,row,i,row,workpass,worksym);
  681.       return(-1);
  682.    }
  683.    return(0);
  684. }
  685.  
  686. static int _fastcall StandardCalc(int passnum)
  687. {
  688.    got_status = 0;
  689.    curpass = passnum;
  690.    row = yybegin;
  691.    while (row <= iystop)
  692.    {
  693.       currow = row;
  694.       reset_periodicity = 1;
  695.       col = ixstart;
  696.       while (col <= ixstop)
  697.       {
  698.      /* on 2nd pass of two, skip even pts */
  699.      if (passnum == 1 || stdcalcmode == '1' || (row&1) != 0 || (col&1) != 0)
  700.      {
  701.         if ((*calctype)() == -1) /* StandardFractal(), calcmand() or calcmandfp() */
  702.            return(-1); /* interrupted */
  703.         reset_periodicity = 0;
  704.         if (passnum == 1) /* first pass, copy pixel and bump col */
  705.         {
  706.            if ((row&1) == 0 && row < iystop)
  707.            {
  708.           (*plot)(col,row+1,color);
  709.           if ((col&1) == 0 && col < ixstop)
  710.              (*plot)(col+1,row+1,color);
  711.            }
  712.            if ((col&1) == 0 && col < ixstop)
  713.           (*plot)(++col,row,color);
  714.         }
  715.      }
  716.      ++col;
  717.       }
  718.       if (passnum == 1 && (row&1) == 0)
  719.      ++row;
  720.       ++row;
  721.    }
  722.    return(0);
  723. }
  724.  
  725.  
  726. int calcmand(void)        /* fast per pixel 1/2/b/g, called with row & col set */
  727. {
  728.    /* setup values from far array to avoid using es reg in calcmand.asm */
  729.    linitx = lx0[col] + lx1[row];
  730.    linity = ly0[row] + ly1[col];
  731.    if (calcmandasm() >= 0)
  732.    {
  733.       if (LogTable /* map color, but not if maxit & adjusted for inside,etc */
  734.       && (realcoloriter < maxit || (inside < 0 && coloriter == maxit)))
  735.      coloriter = LogTable[(int)min(coloriter, MaxLTSize)];
  736.       color = abs((int)coloriter);
  737.       if (coloriter >= colors) /* don't use color 0 unless from inside/outside */
  738.      if (colors < 16)
  739.         color &= andcolor;
  740.      else
  741.         color = ((color - 1) % andcolor) + 1;  /* skip color zero */
  742.       if(debugflag != 470)
  743.          if(color <= 0 && stdcalcmode == 'b' )   /* fix BTM bug */
  744.             color = 1;
  745.       (*plot) (col, row, color);
  746.    }
  747.    else
  748.       color = (int)coloriter;
  749.    return (color);
  750. }
  751.  
  752. /************************************************************************/
  753. /* added by Wes Loewer - sort of a floating point version of calcmand() */
  754. /* can also handle invert, any rqlim, potflag, zmag, epsilon cross,     */
  755. /* and all the current outside options    -Wes Loewer 11/03/91          */
  756. /************************************************************************/
  757. int calcmandfp(void)
  758. {
  759.    if(invert)
  760.       invertz2(&init);
  761.    else
  762.    {
  763.       init.y = dy0[row]+dy1[col];
  764.       init.x = dx0[col]+dx1[row];
  765.    }
  766.    if (calcmandfpasm() >= 0)
  767.    {
  768.       if (potflag)
  769.      coloriter = potential(magnitude, realcoloriter);
  770.       if (LogTable /* map color, but not if maxit & adjusted for inside,etc */
  771.           && (realcoloriter < maxit || (inside < 0 && coloriter == maxit)))
  772.         coloriter = LogTable[(int)min(coloriter, MaxLTSize)];
  773.        color = abs((int)coloriter);
  774.      if (coloriter >= colors) /* don't use color 0 unless from inside/outside */
  775.         if (colors < 16)
  776.            color &= andcolor;
  777.         else
  778.            color = ((color - 1) % andcolor) + 1;  /* skip color zero */
  779.          if(debugflag != 470)
  780.             if(color == 0 && stdcalcmode == 'b' )   /* fix BTM bug */
  781.                color = 1;
  782.       (*plot) (col, row, color);
  783.    }
  784.    else
  785.       color = (int)coloriter;
  786.    return (color);
  787. }
  788. #define STARTRAILMAX FLT_MAX   /* just a convenient large number */
  789. #define green 2
  790. #define yellow 6
  791. #if 0
  792. #define NUMSAVED 40     /* define this to save periodicity analysis to file */
  793. #endif
  794. #if 0
  795. #define MINSAVEDAND 3   /* if not defined, old method used */ 
  796. #endif
  797. int StandardFractal(void)    /* per pixel 1/2/b/g, called with row & col set */
  798. {
  799. #ifdef NUMSAVED
  800.    _CMPLX savedz[NUMSAVED];
  801.    long caught[NUMSAVED];
  802.    long changed[NUMSAVED];
  803.    int zctr = 0;
  804. #endif
  805.    long savemaxit;
  806.    double tantable[16];
  807.    int hooper = 0;
  808.    double close;
  809.    long lclose;
  810.    long cyclelen = -1;
  811.    long savedcoloriter = 0;
  812.    int caught_a_cycle;
  813.    long savedand;
  814.    int savedincr;    /* for periodicity checking */
  815.    _LCMPLX lsaved;
  816.    int i, attracted;
  817.    _LCMPLX lat;
  818.    _CMPLX  at;
  819.    _CMPLX deriv;
  820.    long dem_color = -1;
  821.    _CMPLX dem_new;
  822.    int dem_overflow_flag = 0;
  823.    close = .01;
  824.    lclose = (long)(close*fudge);
  825.    savemaxit = maxit;
  826. #ifdef NUMSAVED
  827.    for(i=0;i<NUMSAVED;i++)
  828.    {
  829.       caught[i] = 0L;
  830.       changed[i] = 0L;
  831.    }
  832. #endif   
  833.    if(inside == STARTRAIL)
  834.    {
  835.       int i;
  836.       for(i=0;i<16;i++)
  837.      tantable[i] = 0.0;
  838.       maxit = 16;
  839.    }
  840.    if (periodicitycheck == 0 || inside == ZMAG || inside == STARTRAIL)
  841.       oldcoloriter = 2147483647L;     /* don't check periodicity at all */
  842.    else if (inside == PERIOD)    /* for display-periodicity */
  843.       oldcoloriter = (maxit/5)*4;    /* don't check until nearly done */
  844.    else if (reset_periodicity)
  845.       oldcoloriter = 255;        /* don't check periodicity 1st 250 iterations */
  846.  
  847.    /* Jonathan - how about this idea ? skips first saved value which never works */
  848. #ifdef MINSAVEDAND
  849.    if(oldcoloriter < MINSAVEDAND)
  850.       oldcoloriter = MINSAVEDAND;
  851. #else
  852.    if (oldcoloriter < firstsavedand) /* I like it! */
  853.       oldcoloriter = firstsavedand;
  854. #endif   
  855.    /* really fractal specific, but we'll leave it here */
  856.    if (!integerfractal)
  857.    {
  858.       if (useinitorbit == 1)
  859.      saved = initorbit;
  860.       else {
  861.      saved.x = 0;
  862.      saved.y = 0;
  863.       }
  864. #ifdef NUMSAVED
  865.       savedz[zctr++] = saved;
  866. #endif      
  867.       if(bf_math)
  868.       {
  869.          if(decimals > 200)
  870.             kbdcount = -1;
  871.          if (bf_math == BIGNUM)
  872.          {
  873.             clear_bn(bnsaved.x);
  874.             clear_bn(bnsaved.y);
  875.          }
  876.          else if (bf_math == BIGFLT)
  877.          {
  878.             clear_bf(bfsaved.x);
  879.             clear_bf(bfsaved.y);
  880.          }
  881.       }      
  882.       init.y = dy0[row] + dy1[col];
  883.       if (distest)
  884.       {
  885.      if (use_old_distest) {
  886.        rqlim = rqlim_save;
  887.        if (distest != 1 || colors == 2) /* not doing regular outside colors */
  888.         if (rqlim < DEM_BAILOUT)   /* so go straight for dem bailout */
  889.            rqlim = DEM_BAILOUT;
  890.        dem_color = -1;
  891.      }
  892.      dem_overflow_flag = 0;
  893.      deriv.x = 1;
  894.      deriv.y = 0;
  895.      magnitude = 0;
  896.       }
  897.    }
  898.    else
  899.    {
  900.       if (useinitorbit == 1)
  901.      lsaved = linitorbit;
  902.       else {
  903.      lsaved.x = 0;
  904.      lsaved.y = 0;
  905.       }
  906.       linit.y = ly0[row] + ly1[col];
  907.    }
  908.    orbit_ptr = 0;
  909.    coloriter = 0;
  910.    if(fractype==JULIAFP || fractype==JULIA)
  911.       coloriter = -1;
  912.    caught_a_cycle = 0;
  913.    if (inside == PERIOD) {
  914.        savedand = 16;        /* begin checking every 16th cycle */
  915.    } else {
  916.      /* Jonathan - don't understand such a low savedand -- how about this? */
  917. #ifdef MINSAVEDAND
  918.        savedand = MINSAVEDAND;
  919. #else       
  920.        savedand = firstsavedand;        /* begin checking every other cycle */
  921. #endif
  922.    }
  923.    savedincr = 1;        /* start checking the very first time */
  924.  
  925.    if (inside <= BOF60 && inside >= BOF61)
  926.    {
  927.       magnitude = lmagnitud = 0;
  928.       min_orbit = 100000.0;
  929.    }
  930.    overflow = 0;        /* reset integer math overflow flag */
  931.  
  932.    curfractalspecific->per_pixel(); /* initialize the calculations */
  933.  
  934.    attracted = FALSE;
  935.    while (++coloriter < maxit)
  936.    {
  937.       /* calculation of one orbit goes here */
  938.       /* input in "old" -- output in "new" */
  939.    if (coloriter % 2048 == 0)
  940.       if (check_key())
  941.          return (-1);
  942.  
  943.       if (distest)
  944.       {
  945.      double ftemp;
  946.      /* Distance estimator for points near Mandelbrot set */
  947.      /* Original code by Phil Wilson, hacked around by PB */
  948.      /* Algorithms from Peitgen & Saupe, Science of Fractal Images, p.198 */
  949.      if (dem_mandel)
  950.         ftemp = 2 * (old.x * deriv.x - old.y * deriv.y) + 1;
  951.      else
  952.         ftemp = 2 * (old.x * deriv.x - old.y * deriv.y);
  953.      deriv.y = 2 * (old.y * deriv.x + old.x * deriv.y);
  954.      deriv.x = ftemp;
  955.      if (use_old_distest)
  956.         if (sqr(deriv.x)+sqr(deriv.y) > dem_toobig) {
  957.            dem_overflow_flag = 1;
  958.            break;
  959.         }
  960.      else
  961.         if (max(fabs(deriv.x),fabs(deriv.y)) > dem_toobig) {
  962.            dem_overflow_flag = 1;
  963.            break;
  964.         }
  965.      /* if above exit taken, the later test vs dem_delta will place this
  966.             point on the boundary, because mag(old)<bailout just now */
  967.          
  968.      if (curfractalspecific->orbitcalc() || overflow)
  969.      {
  970.       if (use_old_distest) {
  971.        if (dem_color < 0) {
  972.           dem_color = coloriter;
  973.           dem_new = new;
  974.        }
  975.        if (rqlim >= DEM_BAILOUT
  976.        || magnitude >= (rqlim = DEM_BAILOUT)
  977.        || magnitude == 0)
  978.           break;
  979.       }
  980.       else
  981.        break;
  982.      }
  983.      old = new;
  984.       }
  985.       /* the usual case */
  986.       else if ((curfractalspecific->orbitcalc() && inside != STARTRAIL)
  987.               || overflow)
  988.         break;
  989.       if (show_orbit)
  990.      if (!integerfractal)
  991.      {
  992.             if (bf_math == BIGNUM)
  993.                new = cmplxbntofloat(&bnnew);
  994.             else if (bf_math==BIGFLT)
  995.                new = cmplxbftofloat(&bfnew);
  996.             plot_orbit(new.x, new.y, -1);
  997.          }
  998.      else
  999.         iplot_orbit(lnew.x, lnew.y, -1);
  1000.       if( inside < -1)
  1001.       {
  1002.          if (bf_math == BIGNUM)
  1003.             new = cmplxbntofloat(&bnnew);
  1004.          else if (bf_math == BIGFLT)
  1005.             new = cmplxbftofloat(&bfnew);
  1006.          if(inside == STARTRAIL)
  1007.          {
  1008.         if(0 < coloriter && coloriter < 16)
  1009.         {
  1010.            if (integerfractal)
  1011.            {
  1012.               new.x = lnew.x;
  1013.               new.x /= fudge;
  1014.               new.y = lnew.y;
  1015.               new.y /= fudge;
  1016.            }
  1017.  
  1018.            if(new.x > STARTRAILMAX)
  1019.               new.x = STARTRAILMAX;
  1020.            if(new.x < -STARTRAILMAX)
  1021.               new.x = -STARTRAILMAX;
  1022.            if(new.y > STARTRAILMAX)
  1023.               new.y = STARTRAILMAX;
  1024.            if(new.y < -STARTRAILMAX)
  1025.               new.y = -STARTRAILMAX;
  1026.                tempsqrx = new.x * new.x;
  1027.                tempsqry = new.y * new.y;
  1028.                magnitude = tempsqrx + tempsqry;
  1029.                old = new;  
  1030.                {
  1031.                int tmpcolor;
  1032.                tmpcolor = abs((int)coloriter);
  1033.             tantable[tmpcolor-1] = new.y/(new.x+.000001);
  1034.             }
  1035.         }
  1036.          }
  1037.          else if(inside == EPSCROSS)
  1038.          {
  1039.         hooper = 0;
  1040.         if(integerfractal)
  1041.         {
  1042.            if(labs(lnew.x) < lclose)
  1043.            {
  1044.               hooper = 1; /* close to y axis */
  1045.               goto plot_inside;
  1046.            }
  1047.            else if(labs(lnew.y) < lclose)
  1048.            {
  1049.               hooper = 2; /* close to x axis */
  1050.               goto plot_inside;
  1051.            }
  1052.         }
  1053.         else
  1054.         {
  1055.            if(fabs(new.x) < close)
  1056.            {
  1057.               hooper = 1; /* close to y axis */
  1058.               goto plot_inside;
  1059.            }
  1060.            else if(fabs(new.y) < close)
  1061.            {
  1062.               hooper = 2; /* close to x axis */
  1063.               goto plot_inside;
  1064.            }
  1065.         }
  1066.          }
  1067.          else if (inside <= BOF60 && inside >= BOF61)
  1068.          {
  1069.         if (integerfractal)
  1070.         {
  1071.            if (lmagnitud == 0 || no_mag_calc == 0)
  1072.               lmagnitud = lsqr(lnew.x) + lsqr(lnew.y);
  1073.            magnitude = lmagnitud;
  1074.            magnitude = magnitude / fudge;
  1075.         }
  1076.         else
  1077.            if (magnitude == 0.0 || no_mag_calc == 0)
  1078.               magnitude = sqr(new.x) + sqr(new.y);
  1079.         if (magnitude < min_orbit)
  1080.         {
  1081.            min_orbit = magnitude;
  1082.            min_index = coloriter + 1;
  1083.         }
  1084.          }
  1085.       }
  1086.  
  1087.  
  1088.       if (attractors > 0)    /* finite attractor in the list   */
  1089.       {             /* NOTE: Integer code is UNTESTED */
  1090.      if (integerfractal)
  1091.      {
  1092.         for (i = 0; i < attractors; i++)
  1093.         {
  1094.         lat.x = lnew.x - lattr[i].x;
  1095.         lat.x = lsqr(lat.x);
  1096.         if (lat.x < l_at_rad)
  1097.         {
  1098.            lat.y = lnew.y - lattr[i].y;
  1099.            lat.y = lsqr(lat.y);
  1100.            if (lat.y < l_at_rad)
  1101.            {
  1102.               if ((lat.x + lat.y) < l_at_rad)
  1103.               {
  1104.              attracted = TRUE;
  1105.              if (finattract<0) coloriter = (coloriter%attrperiod[i])+1;
  1106.              break;
  1107.               }
  1108.            }
  1109.         }
  1110.         }
  1111.      }
  1112.      else
  1113.      {
  1114.         for (i = 0; i < attractors; i++)
  1115.         {
  1116.         at.x = new.x - attr[i].x;
  1117.         at.x = sqr(at.x);
  1118.         if (at.x < f_at_rad)
  1119.         {
  1120.            at.y = new.y - attr[i].y;
  1121.            at.y = sqr(at.y);
  1122.            if ( at.y < f_at_rad)
  1123.            {
  1124.               if ((at.x + at.y) < f_at_rad)
  1125.               {
  1126.              attracted = TRUE;
  1127.              if (finattract<0) coloriter = (coloriter%attrperiod[i])+1;
  1128.              break;
  1129.               }
  1130.            }
  1131.         }
  1132.         }
  1133.      }
  1134.      if (attracted)
  1135.         break;        /* AHA! Eaten by an attractor */
  1136.       }
  1137.  
  1138.       if (coloriter > oldcoloriter) /* check periodicity */
  1139.       {
  1140.      if ((coloriter & savedand) == 0)         /* time to save a new value */
  1141.      {
  1142.         savedcoloriter = coloriter;
  1143.         if (integerfractal)
  1144.            lsaved = lnew;/* integer fractals */
  1145.             else if (bf_math == BIGNUM)
  1146.         {
  1147.                copy_bn(bnsaved.x,bnnew.x);
  1148.                copy_bn(bnsaved.y,bnnew.y);
  1149.             }   
  1150.             else if (bf_math == BIGFLT)
  1151.         {
  1152.                copy_bf(bfsaved.x,bfnew.x);
  1153.                copy_bf(bfsaved.y,bfnew.y);
  1154.             }   
  1155.             else
  1156.         { 
  1157.            saved = new;  /* floating pt fractals */
  1158. #ifdef NUMSAVED
  1159.                if(zctr < NUMSAVED)
  1160.                {
  1161.                   changed[zctr]  = coloriter;
  1162.                   savedz[zctr++] = saved;
  1163.                }
  1164. #endif                  
  1165.             }
  1166.         if (--savedincr == 0)    /* time to lengthen the periodicity? */
  1167.         {
  1168.            savedand = (savedand << 1) + 1;         /* longer periodicity */
  1169.            savedincr = nextsavedincr;/* restart counter */
  1170.         }
  1171.      }
  1172.      else             /* check against an old save */
  1173.            {
  1174.         if (integerfractal)     /* floating-pt periodicity chk */
  1175.         {
  1176.            if (labs(lsaved.x - lnew.x) < lclosenuff)
  1177.           if (labs(lsaved.y - lnew.y) < lclosenuff)
  1178.              caught_a_cycle = 1;
  1179.         }
  1180.             else if (bf_math == BIGNUM)
  1181.         {
  1182.            if (cmp_bn(abs_a_bn(sub_bn(bntmp,bnsaved.x,bnnew.x)), bnclosenuff) < 0)
  1183.               if (cmp_bn(abs_a_bn(sub_bn(bntmp,bnsaved.y,bnnew.y)), bnclosenuff) < 0)
  1184.              caught_a_cycle = 1;
  1185.         }
  1186.             else if (bf_math == BIGFLT)
  1187.         {
  1188.                if (cmp_bf(abs_a_bf(sub_bf(bftmp,bfsaved.x,bfnew.x)), bfclosenuff) < 0)
  1189.                   if (cmp_bn(abs_a_bf(sub_bf(bftmp,bfsaved.y,bfnew.y)), bfclosenuff) < 0)
  1190.              caught_a_cycle = 1;
  1191.         }
  1192.         else
  1193.         {
  1194.            if (fabs(saved.x - new.x) < closenuff)
  1195.           if (fabs(saved.y - new.y) < closenuff)
  1196.              caught_a_cycle = 1;
  1197. #ifdef NUMSAVED
  1198.            int i;
  1199.         for(i=0;i<=zctr;i++)
  1200.         {
  1201.            if(caught[i] == 0)
  1202.            {
  1203.                       if (fabs(savedz[i].x - new.x) < closenuff)
  1204.                  if (fabs(savedz[i].y - new.y) < closenuff)
  1205.                      caught[i] = coloriter;
  1206.            }
  1207.         }
  1208. #endif             
  1209.         }
  1210.         if(caught_a_cycle)
  1211.         {
  1212. #ifdef NUMSAVED
  1213.             char msg[80];
  1214.             static FILE *fp = NULL;
  1215.             static char c;
  1216.             if(fp==NULL)
  1217.                fp = dir_fopen(workdir,"cycles.txt","w");
  1218. #endif
  1219.         cyclelen = coloriter-savedcoloriter;
  1220. #ifdef NUMSAVED
  1221.         fprintf(fp,"row %3d col %3d len %6ld iter %6ld savedand %6ld\n",
  1222.             row,col,cyclelen,coloriter,savedand);
  1223.         if(zctr > 1 && zctr < NUMSAVED)     
  1224.         {
  1225.            int i;
  1226.            for(i=0;i<zctr;i++)
  1227.               fprintf(fp,"   caught %2d saved %6ld iter %6ld\n",i,changed[i],caught[i]);
  1228.         }
  1229.         fflush(fp);    
  1230. #endif
  1231.         coloriter = maxit - 1;
  1232.         }
  1233.         
  1234.      }
  1235.       }
  1236.    }  /* end while (coloriter++ < maxit) */ 
  1237.  
  1238.    if (show_orbit)
  1239.       scrub_orbit();
  1240.  
  1241.    realcoloriter = coloriter;        /* save this before we start adjusting it */
  1242.    if (coloriter >= maxit)
  1243.       oldcoloriter = 0;        /* check periodicity immediately next time */
  1244.    else
  1245.    {
  1246.       oldcoloriter = coloriter + 10;    /* check when past this + 10 next time */
  1247.       if (coloriter == 0)
  1248.      coloriter = 1;        /* needed to make same as calcmand */
  1249.    }
  1250.  
  1251.    if (potflag)
  1252.    {
  1253.       if (integerfractal)    /* adjust integer fractals */
  1254.       {
  1255.      new.x = ((double)lnew.x) / fudge;
  1256.      new.y = ((double)lnew.y) / fudge;
  1257.       }
  1258.       else if (bf_math==BIGNUM)
  1259.       {
  1260.      new.x = (double)bntofloat(bnnew.x);
  1261.      new.y = (double)bntofloat(bnnew.y);
  1262.       }
  1263.       else if (bf_math==BIGFLT)
  1264.       {
  1265.          new.x = (double)bftofloat(bfnew.x);
  1266.          new.y = (double)bftofloat(bfnew.y);
  1267.       }
  1268.       magnitude = sqr(new.x) + sqr(new.y);
  1269.       coloriter = potential(magnitude, coloriter);
  1270.       if (LogTable)
  1271.      coloriter = LogTable[(int)min(coloriter, MaxLTSize)];
  1272.       goto plot_pixel;        /* skip any other adjustments */
  1273.    }
  1274.  
  1275.    if (coloriter >= maxit)        /* an "inside" point */
  1276.       goto plot_inside;     /* distest, decomp, biomorph don't apply */
  1277.  
  1278.  
  1279.    if (outside < -1)  /* these options by Richard Hughes modified by TW */
  1280.    {
  1281.       if (integerfractal)
  1282.       {
  1283.      new.x = ((double)lnew.x) / fudge;
  1284.      new.y = ((double)lnew.y) / fudge;
  1285.       }
  1286.       else if(bf_math==1)
  1287.       {
  1288.      new.x = (double)bntofloat(bnnew.x);
  1289.      new.y = (double)bntofloat(bnnew.y);
  1290.       }
  1291.       /* Add 7 to overcome negative values on the MANDEL    */
  1292.       if (outside == REAL)         /* "real" */
  1293.      coloriter += (long)new.x + 7;
  1294.       else if (outside == IMAG)         /* "imag" */
  1295.      coloriter += (long)new.y + 7;
  1296.       else if (outside == MULT  && new.y)  /* "mult" */
  1297.       coloriter = (long)((double)coloriter * (new.x/new.y));
  1298.       else if (outside == SUM)         /* "sum" */
  1299.       coloriter += (long)(new.x + new.y);
  1300.       else if (outside == ATAN)         /* "atan" */
  1301.       coloriter = (long)fabs(atan2(new.y,new.x)*180.0/PI);
  1302.  
  1303.       /* eliminate negative colors & wrap arounds */
  1304.       if (coloriter < 0 || coloriter > maxit)
  1305.       coloriter = 0;
  1306.    }
  1307.  
  1308.    if (distest)
  1309.    {
  1310.       double dist,temp;
  1311.       if (magnitude == 0)
  1312.          magnitude = sqr(new.x) + sqr(new.y);
  1313.       if (dem_overflow_flag || magnitude == 0)
  1314.          dist = 0;
  1315.       else {
  1316.          temp = log(magnitude);
  1317.          dist = magnitude * sqr(temp) / ( sqr(deriv.x) + sqr(deriv.y) );
  1318.       }
  1319.       if (dist < dem_delta)    /* point is on the edge */
  1320.       {
  1321.      if (distest > 0)
  1322.         goto plot_inside;    /* show it as an inside point */
  1323.      coloriter = 0 - distest;    /* show boundary as specified color */
  1324.      goto plot_pixel;    /* no further adjustments apply */
  1325.       }
  1326.       if (colors == 2)
  1327.       {
  1328.      coloriter = !inside;    /* the only useful distest 2 color use */
  1329.      goto plot_pixel;    /* no further adjustments apply */
  1330.       }
  1331.       if (distest > 1)        /* pick color based on distance */
  1332.       {
  1333.          if (old_demm_colors) /* this one is needed for old color scheme */
  1334.         coloriter = (long)sqrt(sqrt(dist) / dem_width + 1);
  1335.          else if (use_old_distest)
  1336.         coloriter = (long)sqrt(dist / dem_width + 1);
  1337.          else
  1338.         coloriter = (long)(dist / dem_width + 1);
  1339.          coloriter &= LONG_MAX;  /* oops - color can be negative */
  1340.      goto plot_pixel;    /* no further adjustments apply */
  1341.       }
  1342.       if (use_old_distest) {
  1343.          coloriter = dem_color;
  1344.          new = dem_new;
  1345.       }
  1346.       /* use pixel's "regular" color */
  1347.    }
  1348.  
  1349.    if (decomp[0] > 0)
  1350.       decomposition();
  1351.    else if (biomorph != -1)
  1352.    {
  1353.       if (integerfractal)
  1354.       {
  1355.      if (labs(lnew.x) < llimit2 || labs(lnew.y) < llimit2)
  1356.         coloriter = biomorph;
  1357.       }
  1358.       else
  1359.      if (fabs(new.x) < rqlim2 || fabs(new.y) < rqlim2)
  1360.         coloriter = biomorph;
  1361.    }
  1362.  
  1363.    if (outside >= 0 && attracted == FALSE) /* merge escape-time stripes */
  1364.       coloriter = outside;
  1365.    else if (LogTable)
  1366.       coloriter = LogTable[(int)min(coloriter, MaxLTSize)];
  1367.    goto plot_pixel;
  1368.  
  1369.    plot_inside: /* we're "inside" */
  1370.    if (periodicitycheck < 0 && caught_a_cycle)
  1371.       coloriter = 7;           /* show periodicity */
  1372.    else if (inside >= 0)
  1373.       coloriter = inside;           /* set to specified color, ignore logpal */
  1374.    else
  1375.    {
  1376.       if(inside == STARTRAIL)
  1377.       {
  1378.      int i;
  1379.      double diff;
  1380.      coloriter = 0;
  1381.      for(i=1;i<16;i++)
  1382.      {
  1383.         diff = tantable[0] - tantable[i];
  1384.         if(fabs(diff) < .05)
  1385.         {
  1386.            coloriter = i;
  1387.            break;
  1388.         }
  1389.      }
  1390.       }
  1391.       else if(inside== PERIOD) {
  1392.       if (cyclelen>0) {
  1393.           coloriter = cyclelen;
  1394.       } else {
  1395.           coloriter = maxit;
  1396.       }
  1397.       }
  1398.       else if(inside == EPSCROSS)
  1399.       {
  1400.      if(hooper==1)
  1401.         coloriter = green;
  1402.      else if(hooper==2)
  1403.         coloriter = yellow;
  1404.      else
  1405.         coloriter = maxit;
  1406.      if (show_orbit)
  1407.         scrub_orbit();
  1408.       }
  1409.       else if (inside == BOF60)
  1410.      coloriter = (long)(sqrt(min_orbit) * 75);
  1411.       else if (inside == BOF61)
  1412.      coloriter = min_index;
  1413.       else if (inside == ZMAG)
  1414.       {
  1415.      if (integerfractal)
  1416.      {
  1417.         /*
  1418.         new.x = ((double)lnew.x) / fudge;
  1419.         new.y = ((double)lnew.y) / fudge;
  1420.         */
  1421.         coloriter = (long)((((double)lsqr(lnew.x))/fudge + ((double)lsqr(lnew.y))/fudge) * (maxit>>1) + 1);
  1422.      }
  1423.      else
  1424.         coloriter = (long)((sqr(new.x) + sqr(new.y)) * (maxit>>1) + 1);
  1425.       }
  1426.       else /* inside == -1 */
  1427.      coloriter = maxit;
  1428.       if (LogTable)
  1429.      coloriter = LogTable[(int)min(coloriter, MaxLTSize)];
  1430.    }
  1431.  
  1432.    plot_pixel:
  1433.  
  1434.    color = abs((int)coloriter);
  1435.    if (coloriter >= colors) /* don't use color 0 unless from inside/outside */
  1436.       if (colors < 16)
  1437.      color &= andcolor;
  1438.       else
  1439.      color = ((color - 1) % andcolor) + 1;    /* skip color zero */
  1440.    if(debugflag != 470)
  1441.       if(color <= 0 && stdcalcmode == 'b' )   /* fix BTM bug */
  1442.          color = 1;
  1443.    (*plot) (col, row, color);
  1444.    
  1445.    maxit = savemaxit;
  1446.    if ((kbdcount -= abs((int)realcoloriter)) <= 0)
  1447.    {
  1448.       if (check_key())
  1449.      return (-1);
  1450.       kbdcount = max_kbdcount;
  1451.    }
  1452.    return (color);
  1453. }
  1454. #undef green
  1455. #undef yellow
  1456.  
  1457. #define cos45  sin45  
  1458. #define lcos45 lsin45
  1459.  
  1460. /**************** standardfractal doodad subroutines *********************/
  1461. static void decomposition(void)
  1462. {
  1463. /* static double cos45     = 0.70710678118654750; */ /* cos 45    degrees */
  1464.    static double sin45     = 0.70710678118654750; /* sin 45    degrees */
  1465.    static double cos22_5   = 0.92387953251128670; /* cos 22.5    degrees */
  1466.    static double sin22_5   = 0.38268343236508980; /* sin 22.5    degrees */
  1467.    static double cos11_25  = 0.98078528040323040; /* cos 11.25    degrees */
  1468.    static double sin11_25  = 0.19509032201612820; /* sin 11.25    degrees */
  1469.    static double cos5_625  = 0.99518472667219690; /* cos 5.625    degrees */
  1470.    static double sin5_625  = 0.09801714032956060; /* sin 5.625    degrees */
  1471.    static double tan22_5   = 0.41421356237309500; /* tan 22.5    degrees */
  1472.    static double tan11_25  = 0.19891236737965800; /* tan 11.25    degrees */
  1473.    static double tan5_625  = 0.09849140335716425; /* tan 5.625    degrees */
  1474.    static double tan2_8125 = 0.04912684976946725; /* tan 2.8125 degrees */
  1475.    static double tan1_4063 = 0.02454862210892544; /* tan 1.4063 degrees */
  1476. /* static long lcos45     ;*/ /* cos 45      degrees */
  1477.    static long lsin45      ; /* sin 45      degrees */
  1478.    static long lcos22_5   ; /* cos 22.5   degrees */
  1479.    static long lsin22_5   ; /* sin 22.5   degrees */
  1480.    static long lcos11_25  ; /* cos 11.25  degrees */
  1481.    static long lsin11_25  ; /* sin 11.25  degrees */
  1482.    static long lcos5_625  ; /* cos 5.625  degrees */
  1483.    static long lsin5_625  ; /* sin 5.625  degrees */
  1484.    static long ltan22_5   ; /* tan 22.5   degrees */
  1485.    static long ltan11_25  ; /* tan 11.25  degrees */
  1486.    static long ltan5_625  ; /* tan 5.625  degrees */
  1487.    static long ltan2_8125 ; /* tan 2.8125 degrees */
  1488.    static long ltan1_4063 ; /* tan 1.4063 degrees */
  1489.    static long reset_fudge = -1;
  1490.    int temp = 0;
  1491.    int save_temp = 0;
  1492.    int i;
  1493.    _LCMPLX lalt;
  1494.    _CMPLX alt;
  1495.    coloriter = 0;
  1496.    if (integerfractal) /* the only case */
  1497.    {
  1498.       if (reset_fudge != fudge)
  1499.       {
  1500.      reset_fudge = fudge;
  1501.      /* lcos45     = (long)(cos45 * fudge); */
  1502.      lsin45     = (long)(sin45 * fudge);
  1503.      lcos22_5   = (long)(cos22_5 * fudge);
  1504.      lsin22_5   = (long)(sin22_5 * fudge);
  1505.      lcos11_25  = (long)(cos11_25 * fudge);
  1506.      lsin11_25  = (long)(sin11_25 * fudge);
  1507.      lcos5_625  = (long)(cos5_625 * fudge);
  1508.      lsin5_625  = (long)(sin5_625 * fudge);
  1509.      ltan22_5   = (long)(tan22_5 * fudge);
  1510.      ltan11_25  = (long)(tan11_25 * fudge);
  1511.      ltan5_625  = (long)(tan5_625 * fudge);
  1512.      ltan2_8125 = (long)(tan2_8125 * fudge);
  1513.      ltan1_4063 = (long)(tan1_4063 * fudge);
  1514.       }
  1515.       if (lnew.y < 0)
  1516.       {
  1517.      temp = 2;
  1518.      lnew.y = -lnew.y;
  1519.       }
  1520.  
  1521.       if (lnew.x < 0)
  1522.       {
  1523.      ++temp;
  1524.      lnew.x = -lnew.x;
  1525.       }
  1526.       if (decomp[0] == 2 && save_release >= 1827)
  1527.       {
  1528.     save_temp = temp;
  1529.     if(temp==2) save_temp = 3;
  1530.     if(temp==3) save_temp = 2;
  1531.       }
  1532.  
  1533.       if (decomp[0] >= 8)
  1534.       {
  1535.      temp <<= 1;
  1536.      if (lnew.x < lnew.y)
  1537.      {
  1538.         ++temp;
  1539.         lalt.x = lnew.x; /* just */
  1540.         lnew.x = lnew.y; /* swap */
  1541.         lnew.y = lalt.x; /* them */
  1542.      }
  1543.  
  1544.      if (decomp[0] >= 16)
  1545.      {
  1546.         temp <<= 1;
  1547.         if (multiply(lnew.x,ltan22_5,bitshift) < lnew.y)
  1548.         {
  1549.            ++temp;
  1550.            lalt = lnew;
  1551.            lnew.x = multiply(lalt.x,lcos45,bitshift) +
  1552.            multiply(lalt.y,lsin45,bitshift);
  1553.            lnew.y = multiply(lalt.x,lsin45,bitshift) -
  1554.            multiply(lalt.y,lcos45,bitshift);
  1555.         }
  1556.  
  1557.         if (decomp[0] >= 32)
  1558.         {
  1559.            temp <<= 1;
  1560.            if (multiply(lnew.x,ltan11_25,bitshift) < lnew.y)
  1561.            {
  1562.           ++temp;
  1563.           lalt = lnew;
  1564.           lnew.x = multiply(lalt.x,lcos22_5,bitshift) +
  1565.               multiply(lalt.y,lsin22_5,bitshift);
  1566.           lnew.y = multiply(lalt.x,lsin22_5,bitshift) -
  1567.               multiply(lalt.y,lcos22_5,bitshift);
  1568.            }
  1569.  
  1570.            if (decomp[0] >= 64)
  1571.            {
  1572.           temp <<= 1;
  1573.           if (multiply(lnew.x,ltan5_625,bitshift) < lnew.y)
  1574.           {
  1575.              ++temp;
  1576.              lalt = lnew;
  1577.              lnew.x = multiply(lalt.x,lcos11_25,bitshift) +
  1578.              multiply(lalt.y,lsin11_25,bitshift);
  1579.              lnew.y = multiply(lalt.x,lsin11_25,bitshift) -
  1580.              multiply(lalt.y,lcos11_25,bitshift);
  1581.           }
  1582.  
  1583.           if (decomp[0] >= 128)
  1584.           {
  1585.              temp <<= 1;
  1586.              if (multiply(lnew.x,ltan2_8125,bitshift) < lnew.y)
  1587.              {
  1588.             ++temp;
  1589.             lalt = lnew;
  1590.             lnew.x = multiply(lalt.x,lcos5_625,bitshift) +
  1591.                 multiply(lalt.y,lsin5_625,bitshift);
  1592.             lnew.y = multiply(lalt.x,lsin5_625,bitshift) -
  1593.                 multiply(lalt.y,lcos5_625,bitshift);
  1594.              }
  1595.  
  1596.              if (decomp[0] == 256)
  1597.              {
  1598.             temp <<= 1;
  1599.             if (multiply(lnew.x,ltan1_4063,bitshift) < lnew.y)
  1600.                if ((lnew.x*ltan1_4063 < lnew.y))
  1601.                   ++temp;
  1602.              }
  1603.           }
  1604.            }
  1605.         }
  1606.      }
  1607.       }
  1608.    }
  1609.    else /* double case */
  1610.    {
  1611.       if (new.y < 0)
  1612.       {
  1613.      temp = 2;
  1614.      new.y = -new.y;
  1615.       }
  1616.       if (new.x < 0)
  1617.       {
  1618.      ++temp;
  1619.      new.x = -new.x;
  1620.       }
  1621.       if (decomp[0] == 2 && save_release >= 1827)
  1622.       {
  1623.     save_temp = temp;
  1624.     if(temp==2) save_temp = 3;
  1625.     if(temp==3) save_temp = 2;
  1626.       }
  1627.       if (decomp[0] >= 8)
  1628.       {
  1629.      temp <<= 1;
  1630.      if (new.x < new.y)
  1631.      {
  1632.         ++temp;
  1633.         alt.x = new.x; /* just */
  1634.         new.x = new.y; /* swap */
  1635.         new.y = alt.x; /* them */
  1636.      }
  1637.      if (decomp[0] >= 16)
  1638.      {
  1639.         temp <<= 1;
  1640.         if (new.x*tan22_5 < new.y)
  1641.         {
  1642.            ++temp;
  1643.            alt = new;
  1644.            new.x = alt.x*cos45 + alt.y*sin45;
  1645.            new.y = alt.x*sin45 - alt.y*cos45;
  1646.         }
  1647.  
  1648.         if (decomp[0] >= 32)
  1649.         {
  1650.            temp <<= 1;
  1651.            if (new.x*tan11_25 < new.y)
  1652.            {
  1653.           ++temp;
  1654.           alt = new;
  1655.           new.x = alt.x*cos22_5 + alt.y*sin22_5;
  1656.           new.y = alt.x*sin22_5 - alt.y*cos22_5;
  1657.            }
  1658.  
  1659.            if (decomp[0] >= 64)
  1660.            {
  1661.           temp <<= 1;
  1662.           if (new.x*tan5_625 < new.y)
  1663.           {
  1664.              ++temp;
  1665.              alt = new;
  1666.              new.x = alt.x*cos11_25 + alt.y*sin11_25;
  1667.              new.y = alt.x*sin11_25 - alt.y*cos11_25;
  1668.           }
  1669.  
  1670.           if (decomp[0] >= 128)
  1671.           {
  1672.              temp <<= 1;
  1673.              if (new.x*tan2_8125 < new.y)
  1674.              {
  1675.             ++temp;
  1676.             alt = new;
  1677.             new.x = alt.x*cos5_625 + alt.y*sin5_625;
  1678.             new.y = alt.x*sin5_625 - alt.y*cos5_625;
  1679.              }
  1680.  
  1681.              if (decomp[0] == 256)
  1682.              {
  1683.             temp <<= 1;
  1684.             if ((new.x*tan1_4063 < new.y))
  1685.                ++temp;
  1686.              }
  1687.           }
  1688.            }
  1689.         }
  1690.      }
  1691.       }
  1692.    }
  1693.    for (i = 1; temp > 0; ++i)
  1694.    {
  1695.       if (temp & 1)
  1696.      coloriter = (1 << i) - 1 - coloriter;
  1697.       temp >>= 1;
  1698.    }
  1699.    if (decomp[0] == 2 && save_release >= 1827) {
  1700.       if(save_temp & 2) coloriter = 1;
  1701.       else coloriter = 0;
  1702.       if (colors == 2)
  1703.           coloriter++;
  1704.    }
  1705.    else if (decomp[0] == 2 && save_release < 1827)
  1706.       coloriter &= 1;
  1707.    if (colors > decomp[0])
  1708.       coloriter++;
  1709. }
  1710.  
  1711. /******************************************************************/
  1712. /* Continuous potential calculation for Mandelbrot and Julia      */
  1713. /* Reference: Science of Fractal Images p. 190.           */
  1714. /* Special thanks to Mark Peterson for his "MtMand" program that  */
  1715. /* beautifully approximates plate 25 (same reference) and spurred */
  1716. /* on the inclusion of similar capabilities in FRACTINT.      */
  1717. /*                                  */
  1718. /* The purpose of this function is to calculate a color value      */
  1719. /* for a fractal that varies continuously with the screen pixels  */
  1720. /* locations for better rendering in 3D.              */
  1721. /*                                  */
  1722. /* Here "magnitude" is the modulus of the orbit value at          */
  1723. /* "iterations". The potparms[] are user-entered paramters        */
  1724. /* controlling the level and slope of the continuous potential      */
  1725. /* surface. Returns color.  - Tim Wegner 6/25/89          */
  1726. /*                                  */
  1727. /*               -- Change history --              */
  1728. /*                                  */
  1729. /* 09/12/89   - added floatflag support and fixed float underflow */
  1730. /*                                  */
  1731. /******************************************************************/
  1732.  
  1733. static int _fastcall potential(double mag, long iterations)
  1734. {
  1735.    float f_mag,f_tmp,pot;
  1736.    double d_tmp;
  1737.    int i_pot;
  1738.    long l_pot;
  1739.  
  1740.    if(iterations < maxit)
  1741.    {
  1742.       pot = (float)(l_pot = iterations+2);
  1743.       if(l_pot <= 0 || mag <= 1.0)
  1744.      pot = (float)0.0;
  1745.       else /* pot = log(mag) / pow(2.0, (double)pot); */
  1746.       {
  1747.      if(l_pot < 120 && !floatflag) /* empirically determined limit of fShift */
  1748.      {
  1749.         f_mag = (float)mag;
  1750.         fLog14(f_mag,f_tmp); /* this SHOULD be non-negative */
  1751.         fShift(f_tmp,(char)-l_pot,pot);
  1752.      }
  1753.      else
  1754.      {
  1755.         d_tmp = log(mag)/(double)pow(2.0,(double)pot);
  1756.         if(d_tmp > FLT_MIN) /* prevent float type underflow */
  1757.            pot = (float)d_tmp;
  1758.         else
  1759.            pot = (float)0.0;
  1760.      }
  1761.       }
  1762.       /* following transformation strictly for aesthetic reasons */
  1763.       /* meaning of parameters:
  1764.         potparam[0] -- zero potential level - highest color -
  1765.         potparam[1] -- slope multiplier -- higher is steeper
  1766.         potparam[2] -- rqlim value if changeable (bailout for modulus) */
  1767.  
  1768.       if(pot > 0.0)
  1769.       {
  1770.      if(floatflag)
  1771.         pot = (float)sqrt((double)pot);
  1772.      else
  1773.      {
  1774.         fSqrt14(pot,f_tmp);
  1775.         pot = f_tmp;
  1776.      }
  1777.      pot = (float)(potparam[0] - pot*potparam[1] - 1.0);
  1778.       }
  1779.       else
  1780.      pot = (float)(potparam[0] - 1.0);
  1781.       if(pot < 1.0)
  1782.      pot = (float)1.0; /* avoid color 0 */
  1783.    }
  1784.    else if(inside >= 0)
  1785.       pot = inside;
  1786.    else /* inside < 0 implies inside=maxit, so use 1st pot param instead */
  1787.       pot = (float)potparam[0];
  1788.  
  1789.    i_pot = (int)((l_pot = (long)(pot * 256)) >> 8);
  1790.    if(i_pot >= colors)
  1791.    {
  1792.       i_pot = colors - 1;
  1793.       l_pot = 255;
  1794.    }
  1795.  
  1796.    if(pot16bit)
  1797.    {
  1798.       if (dotmode != 11) /* if putcolor won't be doing it for us */
  1799.      writedisk(col+sxoffs,row+syoffs,i_pot);
  1800.       writedisk(col+sxoffs,row+sydots+syoffs,(int)l_pot);
  1801.    }
  1802.  
  1803.    return(i_pot);
  1804. }
  1805.  
  1806.  
  1807. /******************* boundary trace method ***************************
  1808. Fractint's original btm was written by David Guenther.  There were a few
  1809. rare circumstances in which the original btm would not trace or fill
  1810. correctly, even on Mandelbrot Sets.  The code below was adapted from
  1811. "Mandelbrot Sets by Wesley Loewer" (see calmanfp.asm) which was written
  1812. before I was introduced to Fractint.  It should be noted that without
  1813. David Guenther's implimentation of a btm, I doubt that I would have been
  1814. able to impliment my own code into Fractint.  There are several things in
  1815. the following code that are not original with me but came from David
  1816. Guenther's code.  I've noted these places with the initials DG.
  1817.  
  1818.                     Wesley Loewer 3/8/92
  1819. *********************************************************************/
  1820. #define bkcolor 0  /* I have some ideas for the future with this. -Wes */
  1821. #define advance_match()     coming_from = ((going_to = (going_to - 1) & 0x03) - 1) & 0x03
  1822. #define advance_no_match()  going_to = (going_to + 1) & 0x03
  1823.  
  1824. static
  1825. int  bound_trace_main(void)
  1826.     {
  1827.     enum direction coming_from;
  1828.     unsigned int match_found, continue_loop;
  1829.     int trail_color, fillcolor_used, last_fillcolor_used = -1;
  1830.     int max_putline_length;
  1831.     int right, left, length;
  1832.     static FCODE btm_cantbeused[]={"Boundary tracing cannot be used with "};
  1833.     if (inside == 0 || outside == 0)
  1834.     {
  1835.         static FCODE inside_outside[] = {"inside=0 or outside=0"};
  1836.     char msg[80];
  1837.     far_strcpy(msg,btm_cantbeused);
  1838.     far_strcat(msg,inside_outside);
  1839.     stopmsg(0,msg);
  1840.     return(-1);
  1841.     }
  1842.     if (colors < 16)
  1843.     {
  1844.     char msg[80];
  1845.         static FCODE lessthansixteen[] = {"< 16 colors"};
  1846.     far_strcpy(msg,btm_cantbeused);
  1847.     far_strcat(msg,lessthansixteen);
  1848.     stopmsg(0,msg);
  1849.     return(-1);
  1850.     }
  1851.  
  1852.     got_status = 2;
  1853.     max_putline_length = 0; /* reset max_putline_length */
  1854.     for (currow = iystart; currow <= iystop; currow++)
  1855.     {
  1856.     reset_periodicity = 1; /* reset for a new row */
  1857.     color = bkcolor;
  1858.     for (curcol = ixstart; curcol <= ixstop; curcol++)
  1859.         {
  1860.         if (getcolor(curcol, currow) != bkcolor)
  1861.         continue;
  1862.  
  1863.         trail_color = color;
  1864.         row = currow;
  1865.         col = curcol;
  1866.         if ((*calctype)()== -1) /* color, row, col are global */
  1867.         {
  1868.         if (iystop != yystop)  /* DG */
  1869.            iystop = yystop - (currow - yystart); /* allow for sym */
  1870.         add_worklist(xxstart,xxstop,currow,iystop,currow,0,worksym);
  1871.         return -1;
  1872.         }
  1873.         reset_periodicity = 0; /* normal periodicity checking */
  1874.  
  1875.         /*
  1876.         This next line may cause a few more pixels to be calculated,
  1877.         but at the savings of quite a bit of overhead
  1878.         */
  1879.         if (color != trail_color)  /* DG */
  1880.         continue;
  1881.  
  1882.         /* sweep clockwise to trace outline */
  1883.         trail_row = currow;
  1884.         trail_col = curcol;
  1885.         trail_color = color;
  1886.         fillcolor_used = fillcolor > 0 ? fillcolor : trail_color;
  1887.         coming_from = West;
  1888.         going_to = East;
  1889.         match_found = 0;
  1890.         continue_loop = TRUE;
  1891.         do
  1892.         {
  1893.         step_col_row();
  1894.         if (row >= currow
  1895.             && col >= ixstart
  1896.             && col <= ixstop
  1897.             && row <= iystop)
  1898.             {
  1899.             /* the order of operations in this next line is critical */
  1900.             if ((color = getcolor(col, row)) == bkcolor && (*calctype)()== -1)
  1901.                 /* color, row, col are global for (*calctype)() */
  1902.             {
  1903.             if (iystop != yystop)  /* DG */
  1904.                iystop = yystop - (currow - yystart); /* allow for sym */
  1905.             add_worklist(xxstart,xxstop,currow,iystop,currow,0,worksym);
  1906.             return -1;
  1907.             }
  1908.             else if (color == trail_color)
  1909.             {
  1910.             if (match_found < 4) /* to keep it from overflowing */
  1911.                 match_found++;
  1912.             trail_row = row;
  1913.             trail_col = col;
  1914.             advance_match();
  1915.             }
  1916.             else
  1917.             {
  1918.             advance_no_match();
  1919.             continue_loop = going_to != coming_from || match_found;
  1920.             }
  1921.             }
  1922.         else
  1923.             {
  1924.             advance_no_match();
  1925.             continue_loop = going_to != coming_from || match_found;
  1926.             }
  1927.         } while (continue_loop && (col != curcol || row != currow));
  1928.  
  1929.         if (match_found <= 3)  /* DG */
  1930.         { /* no hole */
  1931.         color = bkcolor;
  1932.         reset_periodicity = 1;
  1933.         continue;
  1934.         }
  1935.  
  1936. /*
  1937. Fill in region by looping around again, filling lines to the left
  1938. whenever going_to is South or West
  1939. */
  1940.         trail_row = currow;
  1941.         trail_col = curcol;
  1942.         coming_from = West;
  1943.         going_to = East;
  1944.         do
  1945.         {
  1946.         match_found = FALSE;
  1947.         do
  1948.             {
  1949.             step_col_row();
  1950.             if (row >= currow
  1951.                 && col >= ixstart
  1952.                 && col <= ixstop
  1953.                 && row <= iystop
  1954.                 && getcolor(col,row) == trail_color)
  1955.                   /* getcolor() must be last */
  1956.             {
  1957.             if (going_to == South
  1958.                 || (going_to == West && coming_from != East))
  1959.                 { /* fill a row, but only once */
  1960.                 right = col;
  1961.                 while (--right >= ixstart && (color = getcolor(right,row)) == trail_color)
  1962.                 ; /* do nothing */
  1963.                 if (color == bkcolor) /* check last color */
  1964.                 {
  1965.                 left = right;
  1966.                 while (getcolor(--left,row) == bkcolor)
  1967.                       /* Should NOT be possible for left < ixstart */
  1968.                     ; /* do nothing */
  1969.                 left++; /* one pixel too far */
  1970.                 if (right == left) /* only one hole */
  1971.                     (*plot)(left,row,fillcolor_used);
  1972.                 else
  1973.                     { /* fill the line to the left */
  1974.                     length=right-left+1;
  1975.                     if (fillcolor_used != last_fillcolor_used || length > max_putline_length)
  1976.                     { /* only reset dstack if necessary */
  1977.                     memset(dstack,fillcolor_used,length);
  1978.                     last_fillcolor_used = fillcolor_used;
  1979.                     max_putline_length = length;
  1980.                     }
  1981.                     put_line(row,left,right,dstack);
  1982.                     /* here's where all the symmetry goes */
  1983.                     if (plot == putcolor)
  1984.                     kbdcount -= length >> 4; /* seems like a reasonable value */
  1985.                     else if (plot == symplot2) /* X-axis symmetry */
  1986.                     {
  1987.                     put_line(yystop-(row-yystart),left,right,dstack);
  1988.                     kbdcount -= length >> 3;
  1989.                     }
  1990.                     else if (plot == symplot2Y) /* Y-axis symmetry */
  1991.                     {
  1992.                     put_line(row,xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  1993.                     kbdcount -= length >> 3;
  1994.                     }
  1995.                     else if (plot == symplot2J)  /* Origin symmetry */
  1996.                     {
  1997.                     put_line(yystop-(row-yystart),xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  1998.                     kbdcount -= length >> 3;
  1999.                     }
  2000.                     else if (plot == symplot4) /* X-axis and Y-axis symmetry */
  2001.                     {
  2002.                     put_line(yystop-(row-yystart),left,right,dstack);
  2003.                     put_line(row,xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  2004.                     put_line(yystop-(row-yystart),xxstop-(right-xxstart),xxstop-(left-xxstart),dstack);
  2005.                     kbdcount -= length >> 2;
  2006.                     }
  2007.                     else    /* cheap and easy way out */
  2008.                     {
  2009.                     int c;
  2010.                     for (c = left; c <= right; c++)  /* DG */
  2011.                         (*plot)(c,row,fillcolor_used);
  2012.                     kbdcount -= length >> 1;
  2013.                     }
  2014.                     }
  2015.                                 } /* end of fill line */
  2016.  
  2017. #if 0 /* don't interupt with a check_key() during fill */
  2018.                 if(--kbdcount<=0)
  2019.                 {
  2020.                 if(check_key())
  2021.                     {
  2022.                     if (iystop != yystop)
  2023.                        iystop = yystop - (currow - yystart); /* allow for sym */
  2024.                     add_worklist(xxstart,xxstop,currow,iystop,currow,0,worksym);
  2025.                     return(-1);
  2026.                     }
  2027.                 kbdcount=max_kbdcount;
  2028.                                 }
  2029. #endif
  2030.                 }
  2031.             trail_row = row;
  2032.             trail_col = col;
  2033.             advance_match();
  2034.             match_found = TRUE;
  2035.             }
  2036.             else
  2037.             advance_no_match();
  2038.             } while (!match_found && going_to != coming_from);
  2039.  
  2040.         if (!match_found)
  2041.             { /* next one has to be a match */
  2042.             step_col_row();
  2043.             trail_row = row;
  2044.             trail_col = col;
  2045.             advance_match();
  2046.             }
  2047.         } while (trail_col != curcol || trail_row != currow);
  2048.         reset_periodicity = 1; /* reset after a trace/fill */
  2049.         color = bkcolor;
  2050.         }
  2051.     }
  2052.     return 0;
  2053.     }
  2054.  
  2055. /*******************************************************************/
  2056. /* take one step in the direction of going_to */
  2057. static void step_col_row()
  2058.     {
  2059.     switch (going_to)
  2060.     {
  2061.     case North:
  2062.         col = trail_col;
  2063.         row = trail_row - 1;
  2064.         break;
  2065.     case East:
  2066.         col = trail_col + 1;
  2067.         row = trail_row;
  2068.         break;
  2069.     case South:
  2070.         col = trail_col;
  2071.         row = trail_row + 1;
  2072.         break;
  2073.     case West:
  2074.         col = trail_col - 1;
  2075.         row = trail_row;
  2076.         break;
  2077.     }
  2078.     }
  2079.  
  2080. /******************* end of boundary trace method *******************/
  2081.  
  2082.  
  2083. /************************ super solid guessing *****************************/
  2084.  
  2085. /*
  2086.    I, Timothy Wegner, invented this solidguessing idea and implemented it in
  2087.    more or less the overall framework you see here.  I am adding this note
  2088.    now in a possibly vain attempt to secure my place in history, because
  2089.    Pieter Branderhorst has totally rewritten this routine, incorporating
  2090.    a *MUCH* more sophisticated algorithm.  His revised code is not only
  2091.    faster, but is also more accurate. Harrumph!
  2092. */
  2093.  
  2094. static int solidguess(void)
  2095. {
  2096.    int i,x,y,xlim,ylim,blocksize;
  2097.    unsigned int *pfxp0,*pfxp1;
  2098.    unsigned int u;
  2099.  
  2100.    guessplot=(plot!=putcolor && plot!=symplot2 && plot!=symplot2J);
  2101.    /* check if guessing at bottom & right edges is ok */
  2102.    bottom_guess = (plot == symplot2 || (plot == putcolor && iystop+1 == ydots));
  2103.    right_guess    = (plot == symplot2J
  2104.        || ((plot == putcolor || plot == symplot2) && ixstop+1 == xdots));
  2105.        
  2106.    /* there seems to be a bug in solid guessing at bottom and side */
  2107.    if(debugflag != 472)
  2108.       bottom_guess = right_guess = 0;  /* TIW march 1995 */
  2109.    
  2110.    i = maxblock = blocksize = ssg_blocksize();
  2111.    totpasses = 1;
  2112.    while ((i >>= 1) > 1) ++totpasses;
  2113.  
  2114.    /* ensure window top and left are on required boundary, treat window
  2115.      as larger than it really is if necessary (this is the reason symplot
  2116.      routines must check for > xdots/ydots before plotting sym points) */
  2117.    ixstart &= -1 - (maxblock-1);
  2118.    iystart = yybegin;
  2119.    iystart &= -1 - (maxblock-1);
  2120.  
  2121.    got_status = 1;
  2122.  
  2123.    if (workpass == 0) /* otherwise first pass already done */
  2124.    {
  2125.       /* first pass, calc every blocksize**2 pixel, quarter result & paint it */
  2126.       curpass = 1;
  2127.       if (iystart <= yystart) /* first time for this window, init it */
  2128.       {
  2129.      currow = 0;
  2130.      memset(&tprefix[1][0][0],0,maxxblk*maxyblk*2); /* noskip flags off */
  2131.      reset_periodicity = 1;
  2132.      row=iystart;
  2133.      for(col=ixstart; col<=ixstop; col+=maxblock)
  2134.      { /* calc top row */
  2135.         if((*calctype)()== -1)
  2136.         {
  2137.            add_worklist(xxstart,xxstop,yystart,yystop,yybegin,0,worksym);
  2138.            goto exit_solidguess;
  2139.         }
  2140.         reset_periodicity = 0;
  2141.      }
  2142.       }
  2143.       else
  2144.      memset(&tprefix[1][0][0],-1,maxxblk*maxyblk*2); /* noskip flags on */
  2145.       for(y=iystart; y<=iystop; y+=blocksize)
  2146.       {
  2147.      currow = y;
  2148.      i = 0;
  2149.      if(y+blocksize<=iystop)
  2150.      { /* calc the row below */
  2151.         row=y+blocksize;
  2152.         reset_periodicity = 1;
  2153.         for(col=ixstart; col<=ixstop; col+=maxblock)
  2154.         {
  2155.            if((i=(*calctype)()) == -1)
  2156.           break;
  2157.            reset_periodicity = 0;
  2158.         }
  2159.      }
  2160.      reset_periodicity = 0;
  2161.      if (i == -1 || guessrow(1,y,blocksize) != 0) /* interrupted? */
  2162.      {
  2163.         if (y < yystart)
  2164.            y = yystart;
  2165.         add_worklist(xxstart,xxstop,yystart,yystop,y,0,worksym);
  2166.         goto exit_solidguess;
  2167.      }
  2168.       }
  2169.  
  2170.       if (num_worklist) /* work list not empty, just do 1st pass */
  2171.       {
  2172.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,1,worksym);
  2173.      goto exit_solidguess;
  2174.       }
  2175.       ++workpass;
  2176.       iystart = yystart & (-1 - (maxblock-1));
  2177.  
  2178.       /* calculate skip flags for skippable blocks */
  2179.       xlim=(ixstop+maxblock)/maxblock+1;
  2180.       ylim=((iystop+maxblock)/maxblock+15)/16+1;
  2181.       if(right_guess==0) /* no right edge guessing, zap border */
  2182.      for(y=0;y<=ylim;++y)
  2183.         tprefix[1][y][xlim]= 0xffff;
  2184.       if(bottom_guess==0) /* no bottom edge guessing, zap border */
  2185.       {
  2186.      i=(iystop+maxblock)/maxblock+1;
  2187.      y=i/16+1;
  2188.      i=1<<(i&15);
  2189.      for(x=0;x<=xlim;++x)
  2190.         tprefix[1][y][x]|=i;
  2191.       }
  2192.       /* set each bit in tprefix[0] to OR of it & surrounding 8 in tprefix[1] */
  2193.       for(y=0;++y<ylim;)
  2194.       {
  2195.      pfxp0= (unsigned int *)&tprefix[0][y][0];
  2196.      pfxp1= (unsigned int *)&tprefix[1][y][0];
  2197.      for(x=0;++x<xlim;)
  2198.      {
  2199.         ++pfxp1;
  2200.         u= *(pfxp1-1)|*pfxp1|*(pfxp1+1);
  2201.         *(++pfxp0)=u|(u>>1)|(u<<1)
  2202.            |((*(pfxp1-(maxxblk+1))|*(pfxp1-maxxblk)|*(pfxp1-(maxxblk-1)))>>15)
  2203.           |((*(pfxp1+(maxxblk-1))|*(pfxp1+maxxblk)|*(pfxp1+(maxxblk+1)))<<15);
  2204.      }
  2205.       }
  2206.    }
  2207.    else /* first pass already done */
  2208.       memset(&tprefix[0][0][0],-1,maxxblk*maxyblk*2); /* noskip flags on */
  2209.    if(three_pass)
  2210.       goto exit_solidguess; 
  2211.    /* remaining pass(es), halve blocksize & quarter each blocksize**2 */
  2212.    i = workpass;
  2213.    while (--i > 0) /* allow for already done passes */
  2214.       blocksize = blocksize>>1;
  2215.    reset_periodicity = 0;
  2216.    while((blocksize=blocksize>>1)>=2)
  2217.    {
  2218.       curpass = workpass + 1;
  2219.       for(y=iystart; y<=iystop; y+=blocksize)
  2220.       {
  2221.      currow = y;
  2222.      if(guessrow(0,y,blocksize) != 0)
  2223.      {
  2224.         if (y < yystart)
  2225.            y = yystart;
  2226.         add_worklist(xxstart,xxstop,yystart,yystop,y,workpass,worksym);
  2227.         goto exit_solidguess;
  2228.      }
  2229.       }
  2230.       ++workpass;
  2231.       if (num_worklist /* work list not empty, do one pass at a time */
  2232.       && blocksize>2) /* if 2, we just did last pass */
  2233.       {
  2234.      add_worklist(xxstart,xxstop,yystart,yystop,yystart,workpass,worksym);
  2235.      goto exit_solidguess;
  2236.       }
  2237.       iystart = yystart & (-1 - (maxblock-1));
  2238.    }
  2239.  
  2240.    exit_solidguess:
  2241.    return(0);
  2242. }
  2243.  
  2244. #define calcadot(c,x,y) { col=x; row=y; if((c=(*calctype)())== -1) return -1; }
  2245.  
  2246. static int _fastcall guessrow(int firstpass,int y,int blocksize)
  2247. {
  2248.    int x,i,j,color;
  2249.    int xplushalf,xplusblock;
  2250.    int ylessblock,ylesshalf,yplushalf,yplusblock;
  2251.    int       c21,c31,c41;     /* cxy is the color of pixel at (x,y) */
  2252.    int c12,c22,c32,c42;     /* where c22 is the topleft corner of */
  2253.    int c13,c23,c33;        /* the block being handled in current */
  2254.    int       c24,    c44;     /* iteration                  */
  2255.    int guessed23,guessed32,guessed33,guessed12,guessed13;
  2256.    int prev11,fix21,fix31;
  2257.    unsigned int *pfxptr,pfxmask;
  2258.    
  2259.    c44 = c41 = c42 = 0;  /* just for warning */
  2260.  
  2261.    halfblock=blocksize>>1;
  2262.    i=y/maxblock;
  2263.    pfxptr= (unsigned int *)&tprefix[firstpass][(i>>4)+1][ixstart/maxblock];
  2264.    pfxmask=1<<(i&15);
  2265.    ylesshalf=y-halfblock;
  2266.    ylessblock=y-blocksize; /* constants, for speed */
  2267.    yplushalf=y+halfblock;
  2268.    yplusblock=y+blocksize;
  2269.    prev11= -1;
  2270.    c24=c12=c13=c22=getcolor(ixstart,y);
  2271.    c31=c21=getcolor(ixstart,(y>0)?ylesshalf:0);
  2272.    if(yplusblock<=iystop)
  2273.       c24=getcolor(ixstart,yplusblock);
  2274.    else if(bottom_guess==0)
  2275.       c24= -1;
  2276.    guessed12=guessed13=0;
  2277.  
  2278.    for(x=ixstart; x<=ixstop;)  /* increment at end, or when doing continue */
  2279.    {
  2280.       if((x&(maxblock-1))==0)  /* time for skip flag stuff */
  2281.       {
  2282.      ++pfxptr;
  2283.      if(firstpass==0 && (*pfxptr&pfxmask)==0)  /* check for fast skip */
  2284.      {
  2285.         /* next useful in testing to make skips visible */
  2286.         /*
  2287.                if(halfblock==1)
  2288.                {
  2289.                   (*plot)(x+1,y,0); (*plot)(x,y+1,0); (*plot)(x+1,y+1,0);
  2290.                   }
  2291.              */
  2292.         x+=maxblock;
  2293.         prev11=c31=c21=c24=c12=c13=c22;
  2294.         guessed12=guessed13=0;
  2295.         continue;
  2296.      }
  2297.       }
  2298.  
  2299.       if(firstpass)  /* 1st pass, paint topleft corner */
  2300.      plotblock(0,x,y,c22);
  2301.       /* setup variables */
  2302.       xplusblock=(xplushalf=x+halfblock)+halfblock;
  2303.       if(xplushalf>ixstop)
  2304.       {
  2305.      if(right_guess==0)
  2306.         c31= -1;
  2307.       }
  2308.       else if(y>0)
  2309.      c31=getcolor(xplushalf,ylesshalf);
  2310.       if(xplusblock<=ixstop)
  2311.       {
  2312.      if(yplusblock<=iystop)
  2313.         c44=getcolor(xplusblock,yplusblock);
  2314.      c41=getcolor(xplusblock,(y>0)?ylesshalf:0);
  2315.      c42=getcolor(xplusblock,y);
  2316.       }
  2317.       else if(right_guess==0)
  2318.      c41=c42=c44= -1;
  2319.       if(yplusblock>iystop)
  2320.      c44=(bottom_guess)?c42:-1;
  2321.  
  2322.       /* guess or calc the remaining 3 quarters of current block */
  2323.       guessed23=guessed32=guessed33=1;
  2324.       c23=c32=c33=c22;
  2325.       if(yplushalf>iystop)
  2326.       {
  2327.      if(bottom_guess==0)
  2328.         c23=c33= -1;
  2329.      guessed23=guessed33= -1;
  2330.       }
  2331.       if(xplushalf>ixstop)
  2332.       {
  2333.      if(right_guess==0)
  2334.         c32=c33= -1;
  2335.      guessed32=guessed33= -1;
  2336.       }
  2337.       for(;;) /* go around till none of 23,32,33 change anymore */
  2338.       {
  2339.      if(guessed33>0
  2340.          && (c33!=c44 || c33!=c42 || c33!=c24 || c33!=c32 || c33!=c23))
  2341.      {
  2342.         calcadot(c33,xplushalf,yplushalf);
  2343.         guessed33=0;
  2344.      }
  2345.      if(guessed32>0
  2346.          && (c32!=c33 || c32!=c42 || c32!=c31 || c32!=c21
  2347.          || c32!=c41 || c32!=c23))
  2348.      {
  2349.         calcadot(c32,xplushalf,y);
  2350.         guessed32=0;
  2351.         continue;
  2352.      }
  2353.      if(guessed23>0
  2354.          && (c23!=c33 || c23!=c24 || c23!=c13 || c23!=c12 || c23!=c32))
  2355.      {
  2356.         calcadot(c23,x,yplushalf);
  2357.         guessed23=0;
  2358.         continue;
  2359.      }
  2360.      break;
  2361.       }
  2362.  
  2363.       if(firstpass) /* note whether any of block's contents were calculated */
  2364.      if(guessed23==0 || guessed32==0 || guessed33==0)
  2365.         *pfxptr|=pfxmask;
  2366.  
  2367.       if(halfblock>1) /* not last pass, check if something to display */
  2368.      if(firstpass)    /* display guessed corners, fill in block */
  2369.      {
  2370.         if(guessplot)
  2371.         {
  2372.            if(guessed23>0)
  2373.           (*plot)(x,yplushalf,c23);
  2374.            if(guessed32>0)
  2375.           (*plot)(xplushalf,y,c32);
  2376.            if(guessed33>0)
  2377.           (*plot)(xplushalf,yplushalf,c33);
  2378.         }
  2379.         plotblock(1,x,yplushalf,c23);
  2380.         plotblock(0,xplushalf,y,c32);
  2381.         plotblock(1,xplushalf,yplushalf,c33);
  2382.      }
  2383.      else  /* repaint changed blocks */
  2384.      {
  2385.         if(c23!=c22)
  2386.            plotblock(-1,x,yplushalf,c23);
  2387.         if(c32!=c22)
  2388.            plotblock(-1,xplushalf,y,c32);
  2389.         if(c33!=c22)
  2390.            plotblock(-1,xplushalf,yplushalf,c33);
  2391.      }
  2392.  
  2393.       /* check if some calcs in this block mean earlier guesses need fixing */
  2394.       fix21=((c22!=c12 || c22!=c32)
  2395.       && c21==c22 && c21==c31 && c21==prev11
  2396.       && y>0
  2397.       && (x==ixstart || c21==getcolor(x-halfblock,ylessblock))
  2398.       && (xplushalf>ixstop || c21==getcolor(xplushalf,ylessblock))
  2399.       && c21==getcolor(x,ylessblock));
  2400.       fix31=(c22!=c32
  2401.       && c31==c22 && c31==c42 && c31==c21 && c31==c41
  2402.       && y>0 && xplushalf<=ixstop
  2403.       && c31==getcolor(xplushalf,ylessblock)
  2404.       && (xplusblock>ixstop || c31==getcolor(xplusblock,ylessblock))
  2405.       && c31==getcolor(x,ylessblock));
  2406.       prev11=c31; /* for next time around */
  2407.       if(fix21)
  2408.       {
  2409.      calcadot(c21,x,ylesshalf);
  2410.      if(halfblock>1 && c21!=c22)
  2411.         plotblock(-1,x,ylesshalf,c21);
  2412.       }
  2413.       if(fix31)
  2414.       {
  2415.      calcadot(c31,xplushalf,ylesshalf);
  2416.      if(halfblock>1 && c31!=c22)
  2417.         plotblock(-1,xplushalf,ylesshalf,c31);
  2418.       }
  2419.       if(c23!=c22)
  2420.       {
  2421.      if(guessed12)
  2422.      {
  2423.         calcadot(c12,x-halfblock,y);
  2424.         if(halfblock>1 && c12!=c22)
  2425.            plotblock(-1,x-halfblock,y,c12);
  2426.      }
  2427.      if(guessed13)
  2428.      {
  2429.         calcadot(c13,x-halfblock,yplushalf);
  2430.         if(halfblock>1 && c13!=c22)
  2431.            plotblock(-1,x-halfblock,yplushalf,c13);
  2432.      }
  2433.       }
  2434.       c22=c42;
  2435.       c24=c44;
  2436.       c13=c33;
  2437.       c31=c21=c41;
  2438.       c12=c32;
  2439.       guessed12=guessed32;
  2440.       guessed13=guessed33;
  2441.       x+=blocksize;
  2442.    } /* end x loop */
  2443.  
  2444.    if(firstpass==0 || guessplot) return 0;
  2445.  
  2446.    /* paint rows the fast way */
  2447.    for(i=0;i<halfblock;++i)
  2448.    {
  2449.       if((j=y+i)<=iystop)
  2450.      put_line(j,xxstart,ixstop,&dstack[xxstart]);
  2451.       if((j=y+i+halfblock)<=iystop)
  2452.      put_line(j,xxstart,ixstop,&dstack[xxstart+MAXPIXELS]);
  2453.       if(keypressed()) return -1;
  2454.    }
  2455.    if(plot!=putcolor)  /* symmetry, just vertical & origin the fast way */
  2456.    {
  2457.       if(plot==symplot2J) /* origin sym, reverse lines */
  2458.      for(i=(ixstop+xxstart+1)/2;--i>=xxstart;)
  2459.      {
  2460.         color=dstack[i];
  2461.         dstack[i]=dstack[j=ixstop-(i-xxstart)];
  2462.         dstack[j]=(BYTE)color;
  2463.         j+=MAXPIXELS;
  2464.         color=dstack[i+MAXPIXELS];
  2465.         dstack[i+MAXPIXELS]=dstack[j];
  2466.         dstack[j]=(BYTE)color;
  2467.      }
  2468.       for(i=0;i<halfblock;++i)
  2469.       {
  2470.      if((j=yystop-(y+i-yystart))>iystop && j<ydots)
  2471.         put_line(j,xxstart,ixstop,&dstack[xxstart]);
  2472.      if((j=yystop-(y+i+halfblock-yystart))>iystop && j<ydots)
  2473.         put_line(j,xxstart,ixstop,&dstack[xxstart+MAXPIXELS]);
  2474.      if(keypressed()) return -1;
  2475.       }
  2476.    }
  2477.    return 0;
  2478. }
  2479.  
  2480. static void _fastcall plotblock(int buildrow,int x,int y,int color)
  2481. {
  2482.    int i,xlim,ylim;
  2483.    if((xlim=x+halfblock)>ixstop)
  2484.       xlim=ixstop+1;
  2485.    if(buildrow>=0 && guessplot==0) /* save it for later put_line */
  2486.    {
  2487.       if(buildrow==0)
  2488.      for(i=x;i<xlim;++i)
  2489.         dstack[i]=(BYTE)color;
  2490.       else
  2491.      for(i=x;i<xlim;++i)
  2492.         dstack[i+MAXPIXELS]=(BYTE)color;
  2493.       if (x>=xxstart) /* when x reduced for alignment, paint those dots too */
  2494.      return; /* the usual case */
  2495.    }
  2496.    /* paint it */
  2497.    if((ylim=y+halfblock)>iystop)
  2498.    {
  2499.       if(y>iystop)
  2500.      return;
  2501.       ylim=iystop+1;
  2502.    }
  2503.    for(i=x;++i<xlim;)
  2504.       (*plot)(i,y,color); /* skip 1st dot on 1st row */
  2505.    while(++y<ylim)
  2506.       for(i=x;i<xlim;++i)
  2507.      (*plot)(i,y,color);
  2508. }
  2509.  
  2510.  
  2511. /************************* symmetry plot setup ************************/
  2512.  
  2513. static int _fastcall xsym_split(int xaxis_row,int xaxis_between)
  2514. {
  2515.    int i;
  2516.    if ((worksym&0x11) == 0x10) /* already decided not sym */
  2517.       return(1);
  2518.    if ((worksym&1) != 0) /* already decided on sym */
  2519.       iystop = (yystart+yystop)/2;
  2520.    else /* new window, decide */
  2521.    {
  2522.       worksym |= 0x10;
  2523.       if (xaxis_row <= yystart || xaxis_row >= yystop)
  2524.      return(1); /* axis not in window */
  2525.       i = xaxis_row + (xaxis_row - yystart);
  2526.       if (xaxis_between)
  2527.      ++i;
  2528.       if (i > yystop) /* split into 2 pieces, bottom has the symmetry */
  2529.       {
  2530.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2531.         return(1);
  2532.      iystop = xaxis_row - (yystop - xaxis_row);
  2533.      if (!xaxis_between)
  2534.         --iystop;
  2535.      add_worklist(xxstart,xxstop,iystop+1,yystop,iystop+1,workpass,0);
  2536.      yystop = iystop;
  2537.      return(1); /* tell set_symmetry no sym for current window */
  2538.       }
  2539.       if (i < yystop) /* split into 2 pieces, top has the symmetry */
  2540.       {
  2541.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2542.         return(1);
  2543.      add_worklist(xxstart,xxstop,i+1,yystop,i+1,workpass,0);
  2544.      yystop = i;
  2545.       }
  2546.       iystop = xaxis_row;
  2547.       worksym |= 1;
  2548.    }
  2549.    symmetry = 0;
  2550.    return(0); /* tell set_symmetry its a go */
  2551. }
  2552.  
  2553. static int _fastcall ysym_split(int yaxis_col,int yaxis_between)
  2554. {
  2555.    int i;
  2556.    if ((worksym&0x22) == 0x20) /* already decided not sym */
  2557.       return(1);
  2558.    if ((worksym&2) != 0) /* already decided on sym */
  2559.       ixstop = (xxstart+xxstop)/2;
  2560.    else /* new window, decide */
  2561.    {
  2562.       worksym |= 0x20;
  2563.       if (yaxis_col <= xxstart || yaxis_col >= xxstop)
  2564.      return(1); /* axis not in window */
  2565.       i = yaxis_col + (yaxis_col - xxstart);
  2566.       if (yaxis_between)
  2567.      ++i;
  2568.       if (i > xxstop) /* split into 2 pieces, right has the symmetry */
  2569.       {
  2570.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2571.         return(1);
  2572.      ixstop = yaxis_col - (xxstop - yaxis_col);
  2573.      if (!yaxis_between)
  2574.         --ixstop;
  2575.      add_worklist(ixstop+1,xxstop,yystart,yystop,yystart,workpass,0);
  2576.      xxstop = ixstop;
  2577.      return(1); /* tell set_symmetry no sym for current window */
  2578.       }
  2579.       if (i < xxstop) /* split into 2 pieces, left has the symmetry */
  2580.       {
  2581.      if (num_worklist >= MAXCALCWORK-1) /* no room to split */
  2582.         return(1);
  2583.      add_worklist(i+1,xxstop,yystart,yystop,yystart,workpass,0);
  2584.      xxstop = i;
  2585.       }
  2586.       ixstop = yaxis_col;
  2587.       worksym |= 2;
  2588.    }
  2589.    symmetry = 0;
  2590.    return(0); /* tell set_symmetry its a go */
  2591. }
  2592.  
  2593. #ifdef _MSC_VER
  2594. #pragma optimize ("ea", off)
  2595. #endif
  2596.  
  2597. #if (_MSC_VER >= 700)
  2598. #pragma code_seg ("calcfra1_text")     /* place following in an overlay */
  2599. #endif
  2600.  
  2601. static void _fastcall setsymmetry(int sym, int uselist) /* set up proper symmetrical plot functions */
  2602. {
  2603.    int i;
  2604.    int parmszero;
  2605.    int xaxis_row, yaxis_col;         /* pixel number for origin */
  2606.    int xaxis_between, yaxis_between; /* if axis between 2 pixels, not on one */
  2607.    int xaxis_on_screen=0, yaxis_on_screen=0;
  2608.    double ftemp;
  2609.    bf_t bft1;
  2610.    int saved;
  2611.    symmetry = 1;
  2612.    if(sym == NOPLOT && forcesymmetry == 999)
  2613.    {
  2614.       plot = noplot;
  2615.       return;
  2616.    }
  2617.    /* NOTE: 16-bit potential disables symmetry */
  2618.    /* also any decomp= option and any inversion not about the origin */
  2619.    /* also any rotation other than 180deg and any off-axis stretch */
  2620.    if(bf_math)
  2621.       if(cmp_bf(bfxmin,bfx3rd) || cmp_bf(bfymin,bfy3rd))
  2622.          return;
  2623.    if ((potflag && pot16bit) || (invert && inversion[2] != 0.0)
  2624.        || decomp[0] != 0
  2625.        || xxmin!=xx3rd || yymin!=yy3rd)
  2626.       return;
  2627.    if(sym != XAXIS && sym != XAXIS_NOPARM && inversion[1] != 0.0 && forcesymmetry == 999)
  2628.       return;
  2629.    if(forcesymmetry < 999)
  2630.       sym = forcesymmetry;
  2631.    else if(forcesymmetry == 1000)
  2632.       forcesymmetry = sym;  /* for backwards compatibility */
  2633.    else if(outside==REAL || outside==IMAG || outside==MULT || outside==SUM
  2634.           || outside==ATAN)
  2635.       return;
  2636.    parmszero = (parm.x == 0.0 && parm.y == 0.0 && useinitorbit != 1);
  2637.    switch (fractype)
  2638.    { case LMANLAMFNFN:      /* These need only P1 checked. */
  2639.      case FPMANLAMFNFN:     /* P2 is used for a switch value */
  2640.      case LMANFNFN:         /* These have NOPARM set in fractalp.c, */
  2641.      case FPMANFNFN:        /* but it only applies to P1. */
  2642.      case FPMANDELZPOWER:   /* or P2 is an exponent */
  2643.      case LMANDELZPOWER:
  2644.      case FPMANZTOZPLUSZPWR:
  2645.      case MARKSMANDEL:
  2646.      case MARKSMANDELFP:
  2647.      case MARKSJULIA:
  2648.      case MARKSJULIAFP:
  2649.        break;
  2650.      default:   /* Check P2 for the rest */
  2651.        parmszero = (parmszero && parm2.x == 0.0 && parm2.y == 0.0);
  2652.    }
  2653.    xaxis_row = yaxis_col = -1;
  2654.    if(bf_math)
  2655.    {
  2656.       saved = save_stack();
  2657.       bft1    = alloc_stack(rbflength+2);
  2658.       xaxis_on_screen = (sign_bf(bfymin) != sign_bf(bfymax));
  2659.       yaxis_on_screen = (sign_bf(bfxmin) != sign_bf(bfxmax));
  2660.    }
  2661.    else
  2662.    {
  2663.       xaxis_on_screen = (sign(yymin) != sign(yymax));
  2664.       yaxis_on_screen = (sign(xxmin) != sign(xxmax));
  2665.    }
  2666.    if (xaxis_on_screen) /* axis is on screen */
  2667.    {
  2668.       if(bf_math)
  2669.       {
  2670.          /* ftemp = (0.0-yymax) / (yymin-yymax); */
  2671.          sub_bf(bft1,bfymin,bfymax);
  2672.          div_bf(bft1,bfymax,bft1);
  2673.          neg_a_bf(bft1);
  2674.          ftemp = (double)bftofloat(bft1);
  2675.       }
  2676.       else
  2677.          ftemp = (0.0-yymax) / (yymin-yymax);
  2678.       ftemp *= (ydots-1);
  2679.       ftemp += 0.25;
  2680.       xaxis_row = (int)ftemp;
  2681.       xaxis_between = (ftemp - xaxis_row >= 0.5);
  2682.       if (uselist == 0 && (!xaxis_between || (xaxis_row+1)*2 != ydots))
  2683.      xaxis_row = -1; /* can't split screen, so dead center or not at all */
  2684.    }
  2685.    if (yaxis_on_screen) /* axis is on screen */
  2686.    {
  2687.       if(bf_math)
  2688.       {
  2689.          /* ftemp = (0.0-xxmin) / (xxmax-xxmin); */
  2690.          sub_bf(bft1,bfxmax,bfxmin);
  2691.          div_bf(bft1,bfxmin,bft1);
  2692.          neg_a_bf(bft1);
  2693.          ftemp = (double)bftofloat(bft1);
  2694.       }
  2695.       else
  2696.          ftemp = (0.0-xxmin) / (xxmax-xxmin);
  2697.       ftemp *= (xdots-1);
  2698.       ftemp += 0.25;
  2699.       yaxis_col = (int)ftemp;
  2700.       yaxis_between = (ftemp - yaxis_col >= 0.5);
  2701.       if (uselist == 0 && (!yaxis_between || (yaxis_col+1)*2 != xdots))
  2702.      yaxis_col = -1; /* can't split screen, so dead center or not at all */
  2703.    }
  2704.    switch(sym)         /* symmetry switch */
  2705.    {
  2706.    case XAXIS_NOREAL:     /* X-axis Symmetry (no real param) */
  2707.       if (parm.x != 0.0) break;
  2708.       goto xsym;
  2709.    case XAXIS_NOIMAG:     /* X-axis Symmetry (no imag param) */
  2710.       if (parm.y != 0.0) break;
  2711.       goto xsym;
  2712.    case XAXIS_NOPARM:                 /* X-axis Symmetry  (no params)*/
  2713.       if (!parmszero)
  2714.      break;
  2715.       xsym:
  2716.    case XAXIS:                 /* X-axis Symmetry */
  2717.       if (xsym_split(xaxis_row,xaxis_between) == 0)
  2718.      if(basin)
  2719.         plot = symplot2basin;
  2720.      else
  2721.         plot = symplot2;
  2722.       break;
  2723.    case YAXIS_NOPARM:                 /* Y-axis Symmetry (No Parms)*/
  2724.       if (!parmszero)
  2725.      break;
  2726.    case YAXIS:                 /* Y-axis Symmetry */
  2727.       if (ysym_split(yaxis_col,yaxis_between) == 0)
  2728.      plot = symplot2Y;
  2729.       break;
  2730.    case XYAXIS_NOPARM:                 /* X-axis AND Y-axis Symmetry (no parms)*/
  2731.       if(!parmszero)
  2732.      break;
  2733.    case XYAXIS:              /* X-axis AND Y-axis Symmetry */
  2734.       xsym_split(xaxis_row,xaxis_between);
  2735.       ysym_split(yaxis_col,yaxis_between);
  2736.       switch (worksym & 3)
  2737.       {
  2738.       case 1: /* just xaxis symmetry */
  2739.      if(basin)
  2740.         plot = symplot2basin;
  2741.      else
  2742.         plot = symplot2 ;
  2743.      break;
  2744.       case 2: /* just yaxis symmetry */
  2745.      if (basin) /* got no routine for this case */
  2746.      {
  2747.         ixstop = xxstop; /* fix what split should not have done */
  2748.         symmetry = 1;
  2749.      }
  2750.      else
  2751.         plot = symplot2Y;
  2752.      break;
  2753.       case 3: /* both axes */
  2754.      if(basin)
  2755.         plot = symplot4basin;
  2756.      else
  2757.         plot = symplot4 ;
  2758.       }
  2759.       break;
  2760.    case ORIGIN_NOPARM:                 /* Origin Symmetry (no parms)*/
  2761.       if (!parmszero)
  2762.      break;
  2763.    case ORIGIN:              /* Origin Symmetry */
  2764.       originsym:
  2765.       if ( xsym_split(xaxis_row,xaxis_between) == 0
  2766.       && ysym_split(yaxis_col,yaxis_between) == 0)
  2767.       {
  2768.      plot = symplot2J;
  2769.      ixstop = xxstop; /* didn't want this changed */
  2770.       }
  2771.       else
  2772.       {
  2773.      iystop = yystop; /* in case first split worked */
  2774.      symmetry = 1;
  2775.      worksym = 0x30; /* let it recombine with others like it */
  2776.       }
  2777.       break;
  2778.    case PI_SYM_NOPARM:
  2779.       if (!parmszero)
  2780.      break;
  2781.    case PI_SYM:              /* PI symmetry */
  2782.       if(bf_math)
  2783.       {
  2784.          if((double)bftofloat(abs_a_bf(sub_bf(bft1,bfxmax,bfxmin))) < PI/4)   
  2785.             break; /* no point in pi symmetry if values too close */
  2786.       }
  2787.       else
  2788.       {
  2789.          if(fabs(xxmax - xxmin) < PI/4)   
  2790.             break; /* no point in pi symmetry if values too close */
  2791.       }
  2792.       if(invert && forcesymmetry == 999)
  2793.      goto originsym;
  2794.       plot = symPIplot ;
  2795.       symmetry = 0;
  2796.       if ( xsym_split(xaxis_row,xaxis_between) == 0
  2797.       && ysym_split(yaxis_col,yaxis_between) == 0)
  2798.      if(parm.y == 0.0)
  2799.         plot = symPIplot4J; /* both axes */
  2800.      else
  2801.         plot = symPIplot2J; /* origin */
  2802.       else
  2803.       {
  2804.      iystop = yystop; /* in case first split worked */
  2805.      worksym = 0x30;  /* don't mark pisym as ysym, just do it unmarked */
  2806.       }
  2807.       if(bf_math)
  2808.       {
  2809.          sub_bf(bft1,bfxmax,bfxmin);
  2810.          abs_a_bf(bft1);
  2811.          pixelpi = (int)((PI/(double)bftofloat(bft1)*xdots)); /* PI in pixels */
  2812.       }
  2813.       else
  2814.          pixelpi = (int)((PI/fabs(xxmax-xxmin))*xdots); /* PI in pixels */
  2815.       
  2816.       if ( (ixstop = xxstart+pixelpi-1 ) > xxstop)
  2817.      ixstop = xxstop;
  2818.       if (plot == symPIplot4J && ixstop > (i = (xxstart+xxstop)/2))
  2819.      ixstop = i;
  2820.       break;
  2821.    default:             /* no symmetry */
  2822.       break;
  2823.    }
  2824.    if(bf_math)
  2825.       restore_stack(saved);
  2826. }
  2827.  
  2828. #ifdef _MSC_VER
  2829. #pragma optimize ("ea", on)
  2830. #endif
  2831.  
  2832. #if (_MSC_VER >= 700)
  2833. #pragma code_seg ()       /* back to normal segment */
  2834. #endif
  2835.  
  2836. /**************** tesseral method by CJLT begins here*********************/
  2837. /*  reworked by PB for speed and resumeability */
  2838.  
  2839. struct tess { /* one of these per box to be done gets stacked */
  2840.    int x1,x2,y1,y2;     /* left/right top/bottom x/y coords  */
  2841.    int top,bot,lft,rgt;  /* edge colors, -1 mixed, -2 unknown */
  2842. };
  2843.  
  2844. static int tesseral(void)
  2845. {
  2846.    struct tess *tp;
  2847.  
  2848.    guessplot = (plot != putcolor && plot != symplot2);
  2849.    tp = (struct tess *)&dstack[0];
  2850.    tp->x1 = ixstart;                  /* set up initial box */
  2851.    tp->x2 = ixstop;
  2852.    tp->y1 = iystart;
  2853.    tp->y2 = iystop;
  2854.  
  2855.    if (workpass == 0) { /* not resuming */
  2856.       tp->top = tessrow(ixstart,ixstop,iystart);     /* Do top row */
  2857.       tp->bot = tessrow(ixstart,ixstop,iystop);      /* Do bottom row */
  2858.       tp->lft = tesscol(ixstart,iystart+1,iystop-1); /* Do left column */
  2859.       tp->rgt = tesscol(ixstop,iystart+1,iystop-1);  /* Do right column */
  2860.       if (check_key()) { /* interrupt before we got properly rolling */
  2861.          add_worklist(xxstart,xxstop,yystart,yystop,yystart,0,worksym);
  2862.          return(-1);
  2863.       }
  2864.    }
  2865.  
  2866.    else { /* resuming, rebuild work stack */
  2867.       int i,mid,curx,cury,xsize,ysize;
  2868.       struct tess *tp2;
  2869.       tp->top = tp->bot = tp->lft = tp->rgt = -2;
  2870.       cury = yybegin & 0xfff;
  2871.       ysize = 1;
  2872.       i = (unsigned)yybegin >> 12;
  2873.       while (--i >= 0) ysize <<= 1;
  2874.       curx = workpass & 0xfff;
  2875.       xsize = 1;
  2876.       i = (unsigned)workpass >> 12;
  2877.       while (--i >= 0) xsize <<= 1;
  2878.       for(;;) {
  2879.          tp2 = tp;
  2880.          if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* next divide down middle */
  2881.             if (tp->x1 == curx && (tp->x2 - tp->x1 - 2) < xsize)
  2882.                break;
  2883.             mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  2884.             if (mid > curx) { /* stack right part */
  2885.                memcpy(++tp,tp2,sizeof(*tp));
  2886.                tp->x2 = mid;
  2887.             }
  2888.             tp2->x1 = mid;
  2889.          }
  2890.          else {                   /* next divide across */
  2891.             if (tp->y1 == cury && (tp->y2 - tp->y1 - 2) < ysize) break;
  2892.             mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  2893.             if (mid > cury) { /* stack bottom part */
  2894.                memcpy(++tp,tp2,sizeof(*tp));
  2895.                tp->y2 = mid;
  2896.             }
  2897.             tp2->y1 = mid;
  2898.          }
  2899.       }
  2900.    }
  2901.  
  2902.    got_status = 4; /* for tab_display */
  2903.  
  2904.    while (tp >= (struct tess *)&dstack[0]) { /* do next box */
  2905.       curcol = tp->x1; /* for tab_display */
  2906.       currow = tp->y1;
  2907.  
  2908.       if (tp->top == -1 || tp->bot == -1 || tp->lft == -1 || tp->rgt == -1)
  2909.          goto tess_split;
  2910.       /* for any edge whose color is unknown, set it */
  2911.       if (tp->top == -2)
  2912.          tp->top = tesschkrow(tp->x1,tp->x2,tp->y1);
  2913.       if (tp->top == -1)
  2914.          goto tess_split;
  2915.       if (tp->bot == -2)
  2916.          tp->bot = tesschkrow(tp->x1,tp->x2,tp->y2);
  2917.       if (tp->bot != tp->top)
  2918.          goto tess_split;
  2919.       if (tp->lft == -2)
  2920.          tp->lft = tesschkcol(tp->x1,tp->y1,tp->y2);
  2921.       if (tp->lft != tp->top)
  2922.          goto tess_split;
  2923.       if (tp->rgt == -2)
  2924.          tp->rgt = tesschkcol(tp->x2,tp->y1,tp->y2);
  2925.       if (tp->rgt != tp->top)
  2926.          goto tess_split;
  2927.  
  2928.       {
  2929.       int mid,midcolor;
  2930.       if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* divide down the middle */
  2931.          mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  2932.          midcolor = tesscol(mid, tp->y1+1, tp->y2-1); /* Do mid column */
  2933.          if (midcolor != tp->top) goto tess_split;
  2934.          }
  2935.       else {                   /* divide across the middle */
  2936.          mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  2937.          midcolor = tessrow(tp->x1+1, tp->x2-1, mid); /* Do mid row */
  2938.          if (midcolor != tp->top) goto tess_split;
  2939.          }
  2940.       }
  2941.  
  2942.       {  /* all 4 edges are the same color, fill in */
  2943.          int i,j;
  2944.          i = 0;
  2945.          if(fillcolor != 0)
  2946.          {
  2947.          if(fillcolor > 0)
  2948.             tp->top = fillcolor %colors;
  2949.          if (guessplot || (j = tp->x2 - tp->x1 - 1) < 2) { /* paint dots */
  2950.             for (col = tp->x1 + 1; col < tp->x2; col++)
  2951.                for (row = tp->y1 + 1; row < tp->y2; row++) {
  2952.                   (*plot)(col,row,tp->top);
  2953.                   if (++i > 500) {
  2954.                      if (check_key()) goto tess_end;
  2955.                      i = 0;
  2956.                   }
  2957.                }
  2958.          }
  2959.          else { /* use put_line for speed */
  2960.             memset(&dstack[MAXPIXELS],tp->top,j);
  2961.             for (row = tp->y1 + 1; row < tp->y2; row++) {
  2962.                put_line(row,tp->x1+1,tp->x2-1,&dstack[MAXPIXELS]);
  2963.                if (plot != putcolor) /* symmetry */
  2964.                   if ((j = yystop-(row-yystart)) > iystop && j < ydots)
  2965.                      put_line(j,tp->x1+1,tp->x2-1,&dstack[MAXPIXELS]);
  2966.                if (++i > 25) {
  2967.                   if (check_key()) goto tess_end;
  2968.                   i = 0;
  2969.                }
  2970.             }
  2971.          }
  2972.          }
  2973.          --tp;
  2974.       }
  2975.       continue;
  2976.  
  2977.       tess_split:
  2978.       {  /* box not surrounded by same color, sub-divide */
  2979.          int mid,midcolor;
  2980.          struct tess *tp2;
  2981.          if (tp->x2 - tp->x1 > tp->y2 - tp->y1) { /* divide down the middle */
  2982.             mid = (tp->x1 + tp->x2) >> 1;         /* Find mid point */
  2983.             midcolor = tesscol(mid, tp->y1+1, tp->y2-1); /* Do mid column */
  2984.             if (midcolor == -3) goto tess_end;
  2985.             if (tp->x2 - mid > 1) {    /* right part >= 1 column */
  2986.                if (tp->top == -1) tp->top = -2;
  2987.                if (tp->bot == -1) tp->bot = -2;
  2988.                tp2 = tp;
  2989.                if (mid - tp->x1 > 1) { /* left part >= 1 col, stack right */
  2990.                   memcpy(++tp,tp2,sizeof(*tp));
  2991.                   tp->x2 = mid;
  2992.                   tp->rgt = midcolor;
  2993.                }
  2994.                tp2->x1 = mid;
  2995.                tp2->lft = midcolor;
  2996.             }
  2997.             else
  2998.                --tp;
  2999.          }
  3000.          else {                   /* divide across the middle */
  3001.             mid = (tp->y1 + tp->y2) >> 1;         /* Find mid point */
  3002.             midcolor = tessrow(tp->x1+1, tp->x2-1, mid); /* Do mid row */
  3003.             if (midcolor == -3) goto tess_end;
  3004.             if (tp->y2 - mid > 1) {    /* bottom part >= 1 column */
  3005.                if (tp->lft == -1) tp->lft = -2;
  3006.                if (tp->rgt == -1) tp->rgt = -2;
  3007.                tp2 = tp;
  3008.                if (mid - tp->y1 > 1) { /* top also >= 1 col, stack bottom */
  3009.                   memcpy(++tp,tp2,sizeof(*tp));
  3010.                   tp->y2 = mid;
  3011.                   tp->bot = midcolor;
  3012.                }
  3013.                tp2->y1 = mid;
  3014.                tp2->top = midcolor;
  3015.             }
  3016.             else
  3017.                --tp;
  3018.          }
  3019.       }
  3020.  
  3021.    }
  3022.  
  3023.    tess_end:
  3024.    if (tp >= (struct tess *)&dstack[0]) { /* didn't complete */
  3025.       int i,xsize,ysize;
  3026.       xsize = ysize = 1;
  3027.       i = 2;
  3028.       while (tp->x2 - tp->x1 - 2 >= i) {
  3029.          i <<= 1;
  3030.          ++xsize;
  3031.       }
  3032.       i = 2;
  3033.       while (tp->y2 - tp->y1 - 2 >= i) {
  3034.          i <<= 1;
  3035.          ++ysize;
  3036.       }
  3037.       add_worklist(xxstart,xxstop,yystart,yystop,
  3038.           (ysize<<12)+tp->y1,(xsize<<12)+tp->x1,worksym);
  3039.       return(-1);
  3040.    }
  3041.    return(0);
  3042.  
  3043. } /* tesseral */
  3044.  
  3045. static int _fastcall tesschkcol(int x,int y1,int y2)
  3046. {
  3047.    int i;
  3048.    i = getcolor(x,++y1);
  3049.    while (--y2 > y1)
  3050.       if (getcolor(x,y2) != i) return -1;
  3051.    return i;
  3052. }
  3053.  
  3054. static int _fastcall tesschkrow(int x1,int x2,int y)
  3055. {
  3056.    int i;
  3057.    i = getcolor(x1,y);
  3058.    while (x2 > x1) {
  3059.       if (getcolor(x2,y) != i) return -1;
  3060.       --x2;
  3061.    }
  3062.    return i;
  3063. }
  3064.  
  3065. static int _fastcall tesscol(int x,int y1,int y2)
  3066. {
  3067.    int colcolor,i;
  3068.    col = x;
  3069.    row = y1;
  3070.    reset_periodicity = 1;
  3071.    colcolor = (*calctype)();
  3072.    reset_periodicity = 0;
  3073.    while (++row <= y2) { /* generate the column */
  3074.       if ((i = (*calctype)()) < 0) return -3;
  3075.       if (i != colcolor) colcolor = -1;
  3076.    }
  3077.    return colcolor;
  3078. }
  3079.  
  3080. static int _fastcall tessrow(int x1,int x2,int y)
  3081. {
  3082.    int rowcolor,i;
  3083.    row = y;
  3084.    col = x1;
  3085.    reset_periodicity = 1;
  3086.    rowcolor = (*calctype)();
  3087.    reset_periodicity = 0;
  3088.    while (++col <= x2) { /* generate the row */
  3089.       if ((i = (*calctype)()) < 0) return -3;
  3090.       if (i != rowcolor) rowcolor = -1;
  3091.    }
  3092.    return rowcolor;
  3093. }
  3094.  
  3095. /* added for testing autologmap() */ /* CAE 9211 fixed missing comment */
  3096. /* insert at end of CALCFRAC.C */
  3097.  
  3098. static int autologmap(void)   /*RB*/
  3099. {  /* calculate round screen edges to avoid wasted colours in logmap */
  3100.  long mincolour;
  3101.  int lag;
  3102.  long old_maxit;
  3103.  mincolour=INT_MAX; /* 32767 */
  3104.  row=iystart;
  3105.  reset_periodicity = 0;
  3106.  old_maxit = maxit;
  3107.  for (col=ixstart;col<ixstop;col++) /* top row */
  3108.     {
  3109.       color=(*calctype)();
  3110.       if (realcoloriter < mincolour) {
  3111.     mincolour=realcoloriter ;
  3112.     maxit = mincolour; /*speedup for when edges overlap lakes */
  3113.     }
  3114.       if ( col >=32 ) (*plot)(col-32,row,0);
  3115.     }                                    /* these lines tidy up for BTM etc */
  3116.     for (lag=32;lag>0;lag--) (*plot)(col-lag,row,0);
  3117.  
  3118.  col=ixstop;
  3119.  for (row=iystart;row<iystop;row++) /* right  side */
  3120.     {
  3121.       color=(*calctype)();
  3122.       if (realcoloriter < mincolour) {
  3123.     mincolour=realcoloriter ;
  3124.     maxit = mincolour; /*speedup for when edges overlap lakes */
  3125.     }
  3126.       if ( row >=32 ) (*plot)(col,row-32,0);
  3127.     }
  3128.     for (lag=32;lag>0;lag--) (*plot)(col,row-lag,0);
  3129.  
  3130.  col=ixstart;
  3131.  for (row=iystart;row<iystop;row++) /* left  side */
  3132.     {
  3133.       color=(*calctype)();
  3134.       if (realcoloriter < mincolour) {
  3135.     mincolour=realcoloriter ;
  3136.     maxit = mincolour; /*speedup for when edges overlap lakes */
  3137.     }
  3138.       if ( row >=32 ) (*plot)(col,row-32,0);
  3139.     }
  3140.     for (lag=32;lag>0;lag--) (*plot)(col,row-lag,0);
  3141.  
  3142.  row=iystop ;
  3143.  for (col=ixstart;col<ixstop;col++) /* bottom row */
  3144.     {
  3145.       color=(*calctype)();
  3146.       if (realcoloriter < mincolour) {
  3147.     mincolour=realcoloriter ;
  3148.     maxit = mincolour; /*speedup for when edges overlap lakes */
  3149.     }
  3150.       if ( col >=32 ) (*plot)(col-32,row,0);
  3151.     }
  3152.     for (lag=32;lag>0;lag--) (*plot)(col-lag,row,0);
  3153.  
  3154.  if (mincolour==2) resuming=1; /* insure autologmap not called again */
  3155.  maxit = old_maxit;
  3156.  
  3157.  return ( (int)mincolour );
  3158. }
  3159.  
  3160. /* Symmetry plot for period PI */
  3161. void _fastcall symPIplot(int x, int y, int color)
  3162. {
  3163.    while(x <= xxstop)
  3164.    {
  3165.       putcolor(x, y, color) ;
  3166.       x += pixelpi;
  3167.    }
  3168. }
  3169. /* Symmetry plot for period PI plus Origin Symmetry */
  3170. void _fastcall symPIplot2J(int x, int y, int color)
  3171. {
  3172.    int i,j;
  3173.    while(x <= xxstop)
  3174.    {
  3175.       putcolor(x, y, color) ;
  3176.       if ((i=yystop-(y-yystart)) > iystop && i < ydots
  3177.       && (j=xxstop-(x-xxstart)) < xdots)
  3178.      putcolor(j, i, color) ;
  3179.       x += pixelpi;
  3180.    }
  3181. }
  3182. /* Symmetry plot for period PI plus Both Axis Symmetry */
  3183. void _fastcall symPIplot4J(int x, int y, int color)
  3184. {
  3185.    int i,j;
  3186.    while(x <= (xxstart+xxstop)/2)
  3187.    {
  3188.       j = xxstop-(x-xxstart);
  3189.       putcolor(       x , y , color) ;
  3190.       if (j < xdots)
  3191.      putcolor(j , y , color) ;
  3192.       if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  3193.       {
  3194.      putcolor(x , i , color) ;
  3195.      if (j < xdots)
  3196.         putcolor(j , i , color) ;
  3197.       }
  3198.       x += pixelpi;
  3199.    }
  3200. }
  3201.  
  3202. /* Symmetry plot for X Axis Symmetry */
  3203. void _fastcall symplot2(int x, int y, int color)
  3204. {
  3205.    int i;
  3206.    putcolor(x, y, color) ;
  3207.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  3208.       putcolor(x, i, color) ;
  3209. }
  3210.  
  3211. /* Symmetry plot for Y Axis Symmetry */
  3212. void _fastcall symplot2Y(int x, int y, int color)
  3213. {
  3214.    int i;
  3215.    putcolor(x, y, color) ;
  3216.    if ((i=xxstop-(x-xxstart)) < xdots)
  3217.       putcolor(i, y, color) ;
  3218. }
  3219.  
  3220. /* Symmetry plot for Origin Symmetry */
  3221. void _fastcall symplot2J(int x, int y, int color)
  3222. {
  3223.    int i,j;
  3224.    putcolor(x, y, color) ;
  3225.    if ((i=yystop-(y-yystart)) > iystop && i < ydots
  3226.        && (j=xxstop-(x-xxstart)) < xdots)
  3227.       putcolor(j, i, color) ;
  3228. }
  3229.  
  3230. /* Symmetry plot for Both Axis Symmetry */
  3231. void _fastcall symplot4(int x, int y, int color)
  3232. {
  3233.    int i,j;
  3234.    j = xxstop-(x-xxstart);
  3235.    putcolor(       x , y, color) ;
  3236.    if (j < xdots)
  3237.       putcolor(    j , y, color) ;
  3238.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  3239.    {
  3240.       putcolor(    x , i, color) ;
  3241.       if (j < xdots)
  3242.      putcolor(j , i, color) ;
  3243.    }
  3244. }
  3245.  
  3246. /* Symmetry plot for X Axis Symmetry - Striped Newtbasin version */
  3247. void _fastcall symplot2basin(int x, int y, int color)
  3248. {
  3249.    int i,stripe;
  3250.    putcolor(x, y, color) ;
  3251.    if(basin==2 && color > 8)
  3252.       stripe=8;
  3253.    else
  3254.       stripe = 0;
  3255.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  3256.    {
  3257.       color -= stripe;              /* reconstruct unstriped color */
  3258.       color = (degree+1-color)%degree+1;  /* symmetrical color */
  3259.       color += stripe;              /* add stripe */
  3260.       putcolor(x, i,color)  ;
  3261.    }
  3262. }
  3263.  
  3264. /* Symmetry plot for Both Axis Symmetry  - Newtbasin version */
  3265. void _fastcall symplot4basin(int x, int y, int color)
  3266. {
  3267.    int i,j,color1,stripe;
  3268.    if(color == 0) /* assumed to be "inside" color */
  3269.    {
  3270.       symplot4(x, y, color);
  3271.       return;
  3272.    }
  3273.    if(basin==2 && color > 8)
  3274.       stripe = 8;
  3275.    else
  3276.       stripe = 0;
  3277.    color -= stripe;          /* reconstruct unstriped color */
  3278.    color1 = degree/2+degree+2 - color;
  3279.    if(color < degree/2+2)
  3280.       color1 = degree/2+2 - color;
  3281.    else
  3282.       color1 = degree/2+degree+2 - color;
  3283.    j = xxstop-(x-xxstart);
  3284.    putcolor(       x, y, color+stripe) ;
  3285.    if (j < xdots)
  3286.       putcolor(    j, y, color1+stripe) ;
  3287.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  3288.    {
  3289.       putcolor(    x, i, stripe + (degree+1 - color)%degree+1) ;
  3290.       if (j < xdots)
  3291.      putcolor(j, i, stripe + (degree+1 - color1)%degree+1) ;
  3292.    }
  3293. }
  3294.  
  3295. /* Do nothing plot!!! */
  3296. #ifdef __CLINT__
  3297. #pragma argsused
  3298. #endif
  3299.  
  3300. void _fastcall noplot(int x,int y,int color)
  3301. {
  3302.    x = y = color = 0;  /* just for warning */
  3303. }
  3304.  
  3305.